@Override public Optional<T> call() throws Exception { @Nullable T result = collection().findOneAndUpdate( convertToBson(criteria), convertToBson(update), options); return Optional.fromNullable(result); } });
@Override public Integer call() { T result = collection().findOneAndUpdate( convertToBson(criteria), convertToBson(update), options); return result == null ? 0 : 1; } });
@Signature public Document findOneAndUpdate(BasicDBObject filter, BasicDBObject update) { return (Document) getWrappedObject().findOneAndUpdate(filter, update); }
private void initializeClusterInstanceVersion() { String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase database = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> instances = database.getCollection(CONSTANTS_TB_INSTS); Bson condition = Filters.eq("_id", this.endpoint); Document increases = new Document(); increases.append("version", 1L); Document document = new Document(); document.append("$inc", increases); FindOneAndUpdateOptions options = new FindOneAndUpdateOptions(); options.upsert(true); Document target = instances.findOneAndUpdate(condition, document, new FindOneAndUpdateOptions().upsert(true)); this.instanceVersion = (target == null) ? 1 : (target.getLong("version") + 1); }
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); } }
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); } }
/** * @return next ticket id */ public int getNextId() { Document find = new Document("_id", "ticketId"); Document increase = new Document("seq", 1); Document update = new Document("$inc", increase); Document result = countersCollection.findOneAndUpdate(find, update); return result.getInteger("seq"); }
@Override public Integer call() { T result = collection().findOneAndUpdate( convertToBson(criteria), convertToBson(update), options); return result == null ? 0 : 1; } });
@Test public void testFineOneAndUpdateNotFound() throws Exception { collection.insertOne(json("_id: 1, a: 1")); Document result = collection.findOneAndUpdate(json("_id: 2"), new Document("$inc", json("a: 1"))); assertThat(result).isNull(); assertThat(collection.countDocuments()).isEqualTo(1); }
@Override @Nullable public TDocument findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update, @NotNull FindOneAndUpdateOptions options) { maybeThrowExceptionBeforeUpdate(); TDocument doc = collection.findOneAndUpdate(filter, update, options); maybeThrowExceptionAfterUpdate(); return doc; }
@Override @Nullable public TDocument findOneAndUpdate(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update) { maybeThrowExceptionBeforeUpdate(); TDocument doc = collection.findOneAndUpdate(clientSession, filter, update); maybeThrowExceptionAfterUpdate(); return doc; }
@Override public void unlock() { // Set lockUtil to now or lockAtLeastUntil whichever is later getCollection().findOneAndUpdate( eq(ID, lockConfiguration.getName()), combine(set(LOCK_UNTIL, Date.from(lockConfiguration.getUnlockTime()))) ); } }
@Override @Nullable public TDocument findOneAndUpdate(@NotNull Bson filter, @NotNull Bson update) { maybeThrowExceptionBeforeUpdate(); TDocument doc = collection.findOneAndUpdate(filter, update); maybeThrowExceptionAfterUpdate(); return doc; }
private void writeClassificationToMongo(List<Classification> classify) { classify.forEach( classification -> { Instance instance = classification.getInstance(); documentsCollection.findOneAndUpdate( Filters.eq(new ObjectId((String) instance.getName())), Updates.set( CLASSIFICATION_FIELD, classification.getLabeling().getBestLabel().toString())); }); }
@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 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 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 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 testFindOneAndUpdateError() throws Exception { collection.insertOne(json("_id: 1, a: 1")); assertThatExceptionOfType(MongoCommandException.class) .isThrownBy(() -> collection.findOneAndUpdate(json("_id: 1"), json("$inc: {_id: 1}"))) .withMessageContaining("Command failed with error 66 (ImmutableField): 'Performing an update on the path '_id' would modify the immutable field '_id'"); }