/** * 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; }
/** * 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; }
private FindOneAndUpdateOptions convertToFindOneAndUpdateOptions(FindAndModifyOptions options, Document fields, Document sort) { FindOneAndUpdateOptions result = new FindOneAndUpdateOptions(); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } result = options.getCollation().map(Collation::toMongoCollation).map(result::collation).orElse(result); return result; } }
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions(); opts.sort(sort); if (options.isUpsert()) { opts.upsert(true); } opts.projection(fields); if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } options.getCollation().map(Collation::toMongoCollation).ifPresent(opts::collation); return collection.findOneAndUpdate(query, update, opts); } }
private FindOneAndUpdateOptions convertToFindOneAndUpdateOptions(FindAndModifyOptions options, Document fields, Document sort) { FindOneAndUpdateOptions result = new FindOneAndUpdateOptions(); result = result.projection(fields).sort(sort).upsert(options.isUpsert()); if (options.isReturnNew()) { result = result.returnDocument(ReturnDocument.AFTER); } else { result = result.returnDocument(ReturnDocument.BEFORE); } result = options.getCollation().map(Collation::toMongoCollation).map(result::collation).orElse(result); return result; } }
public Document doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException { FindOneAndUpdateOptions opts = new FindOneAndUpdateOptions(); opts.sort(sort); if (options.isUpsert()) { opts.upsert(true); } opts.projection(fields); if (options.isReturnNew()) { opts.returnDocument(ReturnDocument.AFTER); } options.getCollation().map(Collation::toMongoCollation).ifPresent(opts::collation); return collection.findOneAndUpdate(query, update, opts); } }
/** * 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; }
/** * 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; }
public synchronized void persistToStore() { if (!config.persistent || lastVal == null) { return; } if (LOG.isDebugEnabled()) { LOG.debug("Persisting lastVal={} to store, collection: {}", lastVal, config.collection); } BasicDBObject updateObj = new BasicDBObject().append("$set", new BasicDBObject(config.field, lastVal)); FindOneAndUpdateOptions options = new FindOneAndUpdateOptions() .returnDocument(ReturnDocument.AFTER); trackingObj = dbCol.findOneAndUpdate(trackingObj, updateObj, options); }
private int getNextValueInSeq(final String counterName, final int numbers) { return MongoDbOperations.doDbOperation(() -> { Document andModify = counters.findOneAndUpdate(new Document("_id", counterName), new Document("$inc", new Document("seq", numbers)),new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER).projection(new Document("seq",1))); return (Integer) andModify.get("seq"); }); }
Document query = new Document("id", 2); Document setOnInsert = new Document(); setOnInsert.put("date", new Date()); setOnInsert.put("reptype", "EOD"); Document update = new Document("$setOnInsert", setOnInsert); FindOneAndUpdateOptions options = new FindOneAndUpdateOptions(); options.returnDocument(ReturnDocument.AFTER); options.upsert(true); db.getCollection("COL1").findOneAndUpdate(query, update, options);
@Override public void mark(String digest) { Document dbFile = filesColl.findOneAndUpdate(Filters.eq(METADATA_PROPERTY_FILENAME, digest), Updates.set(String.format("%s.%s", METADATA_PROPERTY_METADATA, msKey), TRUE), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); if (dbFile != null) { status.numBinaries += 1; status.sizeBinaries += dbFile.getLong(METADATA_PROPERTY_LENGTH); } }
@Test public void testFineOneAndUpdateReturnNew() { collection.insertOne(json("_id: 1, a: 1, b: {c: 1}")); Document query = json("_id: 1"); Document update = json("$inc: {a: 1, 'b.c': 1}"); Document result = collection.findOneAndUpdate(query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 1, a: 2, b: {c: 2}")); }
@Test public void testFindOneAndUpdateUpsertReturnBefore() { Document result = collection.findOneAndUpdate(json("_id: 1"), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.BEFORE)); assertThat(result).isEqualTo(json("")); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: 1")); }
@Test public void testFindOneAndUpdateFields() throws Exception { collection.insertOne(json("_id: 1, a: 1")); Document result = collection.findOneAndUpdate(json("_id: 1"), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 1, a: 2")); }
@Test public void testFineOneAndUpdateMax() { collection.insertOne(json("_id: 1, a: 1, b: {c: 1}")); Document query = json("_id: 1"); Document update = json("$max: {a: 2, 'b.c': 2, d : 'd'}"); Document result = collection.findOneAndUpdate(query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 1, a: 2, b: {c: 2}, d : 'd'")); }
@Test public void testFineOneAndUpdateMin() { collection.insertOne(json("_id: 1, a: 2, b: {c: 1}")); Document query = json("_id: 1"); Document update = json("$min: {a: 1, 'b.c': 2, d : 'd'}"); Document result = collection.findOneAndUpdate(query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 1, a: 1, b: {c: 1}, d : 'd'")); }
@Test public void testFindOneAndUpdateUpsert() { Document result = collection.findOneAndUpdate(json("_id: 1"), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 1, a: 1")); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: 1")); }
@Test public void testFindOneAndUpdateReturnOld() { collection.insertOne(json("_id: 1, a: 1, b: {c: 1}")); Document query = json("_id: 1"); Document update = json("$inc: {a: 1, 'b.c': 1}"); Document result = collection.findOneAndUpdate(query, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.BEFORE)); assertThat(result).isEqualTo(json("_id: 1, a: 1, b: {c: 1}")); assertThat(collection.find(query).first()).isEqualTo(json("_id: 1, a: 2, b: {c: 2}")); }
@Test public void testFindOneAndUpdateSorted() throws Exception { collection.insertOne(json("_id: 1, a: 15")); collection.insertOne(json("_id: 2, a: 10")); collection.insertOne(json("_id: 3, a: 20")); Document order = json("a: 1"); Document result = collection.findOneAndUpdate(json(""), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().sort(order).returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 2, a: 11")); order = json("a: -1"); result = collection.findOneAndUpdate(json(""), json("$inc: {a: 1}"), new FindOneAndUpdateOptions().sort(order).returnDocument(ReturnDocument.AFTER)); assertThat(result).isEqualTo(json("_id: 3, a: 21")); }