public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase db = client.getDatabase("course"); MongoCollection<Document> collection = db.getCollection("deleteTest"); collection.drop(); for (int idx = 1; idx <= 8; idx++) { collection.insertOne(new Document("_id", idx)); } System.out.println("Before any deletes"); List<Document> docs = collection.find().into(new ArrayList<Document>()); for (Document doc : docs) { Helper.prettyPrintJSON(doc); } // delete 2,3,4 collection.deleteMany(and(gte("_id", 2), lt("_id", 5))); System.out.println("removed gte2 lt5"); docs = collection.find().into(new ArrayList<Document>()); for (Document doc : docs) { Helper.prettyPrintJSON(doc); } collection.deleteOne(eq("_id", 8)); System.out.println("Removed id 8"); docs = collection.find().into(new ArrayList<Document>()); for (Document doc : docs) { Helper.prettyPrintJSON(doc); } }
@Override public DeleteResult deleteOne(Bson arg0) { if (tx.started()) { return tx.deleteOne(coll, arg0); } else { return coll.deleteOne(arg0); } }
/** {@inheritDoc} */ @Override public void delete(String uid) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException("Feature identifier cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } collection.deleteOne(BUILDER.getFeatUid(uid)); }
public boolean closeBooking(final String bookingId) { try { final MongoDatabase mdb = MongoDBConnManager.getInstance().getConnection(); final MongoCollection<Document> coll = mdb.getCollection(DBConstants.COLL_BOOKING); final Document booking = new Document(); booking.put(DBConstants.BOOKING_ID, bookingId); coll.deleteOne(booking); } catch (Exception e) { if (e instanceof com.mongodb.MongoTimeoutException) { throw new ApplicationException(MessagesEnum.MONGODB_IS_DOWN.getMessage(), e); } throw new ApplicationException(MessagesEnum.CLOSE_BOOKING_FAILED.getMessage(bookingId), e); } return true; }
/** * Delete a directory * * @param u The owner * @param path The path * @param name The name * @return The newly created directory */ public static Directory deleteDirectory(User u, Directory d) { if (d.name == null) // Root can't be deleted return d; MongoCollection<Document> collection = DataBase.getInstance().getCollection("directories"); BasicDBObject query = new BasicDBObject(); query.put("owner", new ObjectId(u.UID)); query.put("name", d.name); query.put("path", d.path); if (collection.deleteOne(query).wasAcknowledged()) { updateDirectoryDate(u, d.parentUID); return getDirectoryParent(u, d); } else { return null; } }
public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase db = client.getDatabase("course"); MongoCollection<Document> coll = db.getCollection("findWithSortTest"); coll.drop(); for (int i = 0; i < 8; i++) { coll.insertOne(new Document().append("_id", i)); } // coll.deleteMany(Filters.gt("_id", 4)); coll.deleteOne(Filters.eq("_id", 4)); for (Document cur : coll.find().into(new ArrayList<Document>())) { System.out.println(cur); } }
public static void main(String[] args) { MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("course"); MongoCollection<Document> collection = database.getCollection("deleteTest"); collection.drop(); // insert 8 documents, with _id set to the value of the loop variable for (int i = 0; i < 8; i++) { collection.insertOne(new Document().append("_id", i)); } // collection.deleteMany(gt("_id", 4)); collection.deleteOne(eq("_id", 4)); for (Document cur : collection.find().into(new ArrayList<Document>())) { Helpers.printJson(cur); } }
/** * Removes a previously stored calendar from the database. * * @param id ID of the user. * @param calendarName Name of the calendar to be removed. */ public void removeScheduledCalendar(String id, String calendarName) { // Access collection of scheduled calendars. MongoCollection collection = db.getCollection("ScheduledCalendars"); // Remove specified calendar. collection.deleteOne(new Document("name", id + calendarName)); }
// ends the session by deleting it from the sesisons table public void endSession(String sessionID) { sessionsCollection.deleteOne(new Document("_id", sessionID)); }