Java Code Examples for com.mongodb.client.model.FindOneAndUpdateOptions#returnDocument()

The following examples show how to use com.mongodb.client.model.FindOneAndUpdateOptions#returnDocument() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBusMongoClient.java    From DBus with Apache License 2.0 6 votes vote down vote up
public long nextSequence(String name) {
    MongoDatabase mdb = mongoClient.getDatabase("dbus");
    MongoCollection mongoCollection = mdb.getCollection("dbus_sequence");
    Document filter = new Document("_id", name);
    Document update = new Document("$inc", new Document("value", 1L));
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    options.upsert(true);
    options.returnDocument(ReturnDocument.AFTER);
    Document doc = (Document) mongoCollection.findOneAndUpdate(filter, update, options);
    return doc.getLong("value");
}
 
Example 2
Source File: MongoCollectionFindAndModify.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	cfData	update	= getNamedParam(argStruct, "update", null );
	if ( update == null )
		throwException(_session, "please specify update");
	
	cfData	query	= getNamedParam(argStruct, "query", null );
	if ( query == null )
		throwException(_session, "please specify query to update");

	try{
		
		MongoCollection<Document> col = db.getCollection(collection);
		FindOneAndUpdateOptions	findOneAndUpdateOptions	= new FindOneAndUpdateOptions();
		
		if ( getNamedParam(argStruct, "fields", null ) != null )
			findOneAndUpdateOptions.projection( getDocument( getNamedParam(argStruct, "fields", null ) ) );

		if ( getNamedParam(argStruct, "sort", null ) != null )
			findOneAndUpdateOptions.sort( getDocument( getNamedParam(argStruct, "sort", null ) ) );

		findOneAndUpdateOptions.upsert( getNamedBooleanParam(argStruct, "upsert", false ) );
		
		if ( getNamedBooleanParam(argStruct, "returnnew", false ) )
			findOneAndUpdateOptions.returnDocument( ReturnDocument.AFTER );
		
		Document qry = getDocument(query);
		long start = System.currentTimeMillis();
		
		Document result = col.findOneAndUpdate( qry, getDocument(update), findOneAndUpdateOptions );
		
		_session.getDebugRecorder().execMongo(col, "findandmodify", qry, System.currentTimeMillis()-start);
		
		return tagUtils.convertToCfData( (Map)result );

	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}