/** * Atomically remove and return a single document. The returned document is the original document before removal. * * @param query specifies the selection criteria for the modification * @return the document as it was before the modifications * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify */ @Nullable public DBObject findAndRemove(@Nullable final DBObject query) { return findAndModify(query, null, null, true, null, false, false); }
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param update the modifications to apply * @return the document as it was before the modifications * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, final DBObject update) { return findAndModify(query, null, null, false, update, false, false); }
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param sort determines which document the operation will modify if the query selects multiple documents * @param update the modifications to apply * @return pre-modification document * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject sort, final DBObject update) { return findAndModify(query, null, sort, false, update, false, false); }
final boolean bypassDocumentValidation, final long maxTime, final TimeUnit maxTimeUnit) { return findAndModify(query, fields, sort, remove, update, returnNew, upsert, bypassDocumentValidation, maxTime, maxTimeUnit, getWriteConcern());
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param fields a subset of fields to return * @param sort determines which document the operation will modify if the query selects multiple documents * @param remove when true, removes the selected document * @param returnNew when true, returns the modified document rather than the original * @param update the modifications to apply * @param upsert when true, operation creates a new document if the query returns no documents * @param writeConcern the write concern to apply to this operation * @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document * after the changes were made * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @since 2.14 * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject fields, @Nullable final DBObject sort, final boolean remove, final DBObject update, final boolean returnNew, final boolean upsert, final WriteConcern writeConcern){ return findAndModify(query, fields, sort, remove, update, returnNew, upsert, 0L, MILLISECONDS, writeConcern); }
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param fields a subset of fields to return * @param sort determines which document the operation will modify if the query selects multiple documents * @param remove when {@code true}, removes the selected document * @param returnNew when true, returns the modified document rather than the original * @param update the modifications to apply * @param upsert when true, operation creates a new document if the query returns no documents * @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document * after the changes were made * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject fields, @Nullable final DBObject sort, final boolean remove, @Nullable final DBObject update, final boolean returnNew, final boolean upsert) { return findAndModify(query, fields, sort, remove, update, returnNew, upsert, 0L, MILLISECONDS); }
/** * Atomically modify and return a single document. By default, the returned document does not include the modifications made on the * update. * * @param query specifies the selection criteria for the modification * @param fields a subset of fields to return * @param sort determines which document the operation will modify if the query selects multiple documents * @param remove when true, removes the selected document * @param returnNew when true, returns the modified document rather than the original * @param update the modifications to apply * @param upsert when true, operation creates a new document if the query returns no documents * @param maxTime the maximum time that the server will allow this operation to execute before killing it. A non-zero value requires * a server version >= 2.6 * @param maxTimeUnit the unit that maxTime is specified in * @return the document as it was before the modifications, unless {@code returnNew} is true, in which case it returns the document * after the changes were made * @throws WriteConcernException if the write failed due some other failure specific to the update command * @throws MongoException if the operation failed for some other reason * @mongodb.driver.manual reference/command/findAndModify/ Find and Modify * @since 2.12.0 */ @Nullable public DBObject findAndModify(@Nullable final DBObject query, @Nullable final DBObject fields, @Nullable final DBObject sort, final boolean remove, @Nullable final DBObject update, final boolean returnNew, final boolean upsert, final long maxTime, final TimeUnit maxTimeUnit) { return findAndModify(query, fields, sort, remove, update, returnNew, upsert, maxTime, maxTimeUnit, getWriteConcern()); }
@Override public <T> T findAndModify(final Query<T> query, final UpdateOperations<T> operations, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.info("Executing findAndModify(" + dbColl.getName() + ") with update "); } updateForVersioning(query, operations); DBObject res = dbColl.findAndModify(query.getQueryObject(), options.copy() .sort(query.getSortObject()) .projection(query.getFieldsObject()) .update(((UpdateOpsImpl<T>) operations).getOps()) .getOptions()); return res == null ? null : mapper.fromDBObject(this, query.getEntityClass(), res, createCache()); }
@Override public <T> T findAndDelete(final Query<T> query, final FindAndModifyOptions options) { DBCollection dbColl = query.getCollection(); if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } if (LOG.isTraceEnabled()) { LOG.trace("Executing findAndModify(" + dbColl.getName() + ") with delete ..."); } FindAndModifyOptions copy = enforceWriteConcern(options, query.getEntityClass()) .copy() .projection(query.getFieldsObject()) .sort(query.getSortObject()) .returnNew(false) .upsert(false) .remove(true); final DBObject result = dbColl.findAndModify(query.getQueryObject(), copy.getOptions()); return result == null ? null : mapper.fromDBObject(this, query.getEntityClass(), result, createCache()); }
final long maxTime, final TimeUnit maxTimeUnit, final WriteConcern writeConcern) { return findAndModify(query != null ? query : new BasicDBObject(), new DBCollectionFindAndModifyOptions() .projection(fields) .sort(sort)
final long maxTime, final TimeUnit maxTimeUnit, final WriteConcern writeConcern) { return findAndModify(query != null ? query : new BasicDBObject(), new DBCollectionFindAndModifyOptions() .projection(fields) .sort(sort)
private Long incrementAndGetSequence(final String whichId) { final DBObject query = new BasicDBObject(TableColumns._id, whichId); final DBObject update = new BasicDBObject("$inc", new BasicDBObject(TableColumns.SEQ, 1)); final DBObject result = seqCollection.findAndModify(query, update); return (Long) result.get(TableColumns.SEQ); }
@Override protected DBObject runMongoCommand() { return getDBCollection().findAndModify(query, fields, sort, remove, update, returnNew, upsert); } }
@Override protected DBObject runMongoCommand() { return getDBCollection().findAndModify(query, fields, sort, remove, update, returnNew, upsert); } }
@Override public void updateKeyValue(String oid, String key, String value) { DBObject query = new BasicDBObject(FIELD_APPLICATION_OID, oid); DBObject update = new BasicDBObject("$set", new BasicDBObject(key, value).append(FIELD_UPDATED, new Date())); getCollection().findAndModify(query, update); }
@Override public T poll(DBCollection collection, DBObjectSerializer<T> serializer) { DBObject dbObject; if (isEmptyCommand.isEmpty(collection)) { return null; } dbObject = collection.findAndModify(null, null, asc ? ORDER_BY_ASC : ORDER_BY_DESC, true, null, false, false); return serializer.toElement(dbObject); }
@Override public boolean markTouched(DocumentID<MongoType> id, String tag) { MongoQuery mq = new MongoQuery(); mq.requireID(id); DBObject update = new BasicDBObject(MongoDocument.METADATA_KEY+"."+DatabaseDocument.TOUCHED_METADATA_TAG+"."+tag, new Date()); DBObject dbo = getUpdateObject(update); if(documents.findAndModify(mq.toDBObject(), dbo)==null) { return false; } return true; }
public static Object getNextSequence(String name) throws Exception{ MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB("demo"); DBCollection collection = db.getCollection("counters"); BasicDBObject find = new BasicDBObject(); find.put("_id", name); BasicDBObject update = new BasicDBObject(); update.put("$inc", new BasicDBObject("seq", 1)); DBObject obj = collection.findAndModify(find, update); return obj.get("seq");
@Override public void removeAccessToken(String accessToken) { BasicDBObject dbObject = new BasicDBObject(); dbObject.put(ACCESS_TOKEN_ID_NAME, accessToken); DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME); List<DBObject> list = coll.find(dbObject).toArray(); if (list.size() > 0) { DBObject newObject = list.get(0); coll.findAndModify(dbObject, newObject); coll.remove(dbObject); } }
public BasicDBObject findAndModify(String description, DBObject query, DBObject update, DBObject fields, DBObject sort, boolean remove, boolean returnNew, boolean upsert) { if (log.isDebugEnabled()) { log.debug("--"+dbCollection.getName()+"-> "+description+" q="+toString(query)+" u="+toString(update)+" f="+toString(fields)); } BasicDBObject dbObject = (BasicDBObject) dbCollection.findAndModify(query, fields, sort, remove, update, returnNew, upsert); if (log.isDebugEnabled()) { log.debug("<-"+dbCollection.getName()+"-- "+(dbObject!=null ? toString(dbObject) : "null")); } return dbObject; }