com.mongodb.client.model.ReturnDocument Java Examples
The following examples show how to use
com.mongodb.client.model.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: CollectionManagementTest.java From quarkus with Apache License 2.0 | 6 votes |
@Test void findAndUpdate() { ReactiveMongoDatabase database = client.getDatabase(DATABASE); ReactiveMongoCollection<Document> collection = database.getCollection("test"); CompletableFuture.allOf( collection .insertOne(new Document("id", 1).append("name", "superman").append("type", "heroes") .append("stars", 5)) .subscribeAsCompletionStage(), collection.insertOne( new Document("id", 2).append("name", "batman").append("type", "heroes").append("stars", 4)) .subscribeAsCompletionStage(), collection .insertOne(new Document("id", 3).append("name", "frogman").append("type", "villain") .append("stars", 1)) .subscribeAsCompletionStage(), collection.insertOne( new Document("id", 4).append("name", "joker").append("type", "villain").append("stars", 5)) .subscribeAsCompletionStage()) .join(); Document frogman = collection.findOneAndUpdate(new Document("id", 3), inc("stars", 3), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)).await().indefinitely(); Document batman = collection.findOneAndUpdate(new Document("id", 2), inc("stars", -1)).await().indefinitely(); assertThat(frogman).contains(entry("stars", 4), entry("name", "frogman")); // Returned after update assertThat(batman).contains(entry("stars", 4), entry("name", "batman")); // Returned the before update }
Example #2
Source File: DBusMongoClient.java From DBus with Apache License 2.0 | 6 votes |
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 #3
Source File: MongoCollectionFindAndModify.java From openbd-core with GNU General Public License v3.0 | 6 votes |
@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; } }
Example #4
Source File: TestVersionAnnotation.java From morphia with Apache License 2.0 | 6 votes |
@Test public void testFindAndModify() { final Datastore datastore = getDs(); Versioned entity = new Versioned(); entity.setName("Value 1"); Query<Versioned> query = datastore.find(Versioned.class); query.filter(eq("name", "Value 1")); entity = query.modify(set("name", "Value 3")) .execute(new ModifyOptions() .returnDocument(ReturnDocument.AFTER) .upsert(true)); Assert.assertEquals("Value 3", entity.getName()); Assert.assertEquals(1, entity.getVersion().longValue()); Assert.assertNotNull(entity.getId()); }
Example #5
Source File: CollectionManagementTest.java From quarkus with Apache License 2.0 | 5 votes |
@Test void findAndReplace() { ReactiveMongoDatabase database = client.getDatabase(DATABASE); ReactiveMongoCollection<Document> collection = database.getCollection("test"); CompletableFuture.allOf( collection .insertOne(new Document("id", 1).append("name", "superman").append("type", "heroes") .append("stars", 5)) .subscribeAsCompletionStage(), collection.insertOne( new Document("id", 2).append("name", "batman").append("type", "heroes").append("stars", 4)) .subscribeAsCompletionStage(), collection .insertOne(new Document("id", 3).append("name", "frogman").append("type", "villain") .append("stars", 1)) .subscribeAsCompletionStage(), collection.insertOne( new Document("id", 4).append("name", "joker").append("type", "villain").append("stars", 5)) .subscribeAsCompletionStage()) .join(); Document newVillain = new Document("id", 5).append("name", "lex lutor").append("type", "villain") .append("stars", 3); Document newHeroes = new Document("id", 6).append("name", "supergirl").append("type", "heroes") .append("stars", 2); Document frogman = collection.findOneAndReplace(new Document("id", 3), newVillain).await().indefinitely(); Document supergirl = collection.findOneAndReplace(new Document("id", 2), newHeroes, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER)).await().indefinitely(); assertThat(frogman).contains(entry("stars", 1), entry("name", "frogman")); assertThat(supergirl).contains(entry("stars", 2), entry("name", "supergirl")); }
Example #6
Source File: Repositories.java From immutables with Apache License 2.0 | 5 votes |
/** * Configures this modifier so that new (updated) version of document will be returned in * case of successful update. * @see #returningOld() * @return {@code this} modifier for chained invocation */ // safe unchecked: we expect I to be a self type @SuppressWarnings("unchecked") public final M returningNew() { options.returnDocument(ReturnDocument.AFTER); return (M) this; }
Example #7
Source File: Repositories.java From immutables with Apache License 2.0 | 5 votes |
/** * Configures this modifier so that new (updated) version of document will be returned in * case of successful update. * @see #returningOld() * @return {@code this} modifier for chained invocation */ // safe unchecked: we expect I to be a self type @SuppressWarnings("unchecked") public final M returningNew() { options.returnDocument(ReturnDocument.AFTER); return (M) this; }
Example #8
Source File: Repositories.java From immutables with Apache License 2.0 | 3 votes |
/** * Configures this modifier so that old (not updated) version of document will be returned in * case of successful update. * This is default behavior so it may be called only for explanatory reasons. * @see #returningNew() * @return {@code this} modifier for chained invocation */ // safe unchecked: we expect I to be a self type @SuppressWarnings("unchecked") public final M returningOld() { options.returnDocument(ReturnDocument.BEFORE); return (M) this; }
Example #9
Source File: Repositories.java From immutables with Apache License 2.0 | 3 votes |
/** * Configures this modifier so that old (not updated) version of document will be returned in * case of successful update. * This is default behavior so it may be called only for explanatory reasons. * @see #returningNew() * @return {@code this} modifier for chained invocation */ // safe unchecked: we expect I to be a self type @SuppressWarnings("unchecked") public final M returningOld() { options.returnDocument(ReturnDocument.BEFORE); return (M) this; }
Example #10
Source File: Datastore.java From morphia with Apache License 2.0 | 3 votes |
/** * Find the first Entity from the Query, and modify it. * * @param query the query to use when finding entities to update * @param operations the updates to apply to the matched documents * @param <T> the type to query * @return The modified Entity (the result of the update) * @deprecated use {@link Query#modify(UpdateOperations)} instead */ @SuppressWarnings("removal") @Deprecated(since = "2.0", forRemoval = true) default <T> T findAndModify(Query<T> query, dev.morphia.query.UpdateOperations<T> operations) { return query.modify(operations).execute(new ModifyOptions() .returnDocument(ReturnDocument.AFTER)); }
Example #11
Source File: Modify.java From morphia with Apache License 2.0 | 2 votes |
/** * Performs the operation * * @return the operation result */ public T execute() { return execute(new ModifyOptions() .returnDocument(ReturnDocument.AFTER)); }