@Signature public void updateMany(BasicDBObject filter, BasicDBObject update) { getWrappedObject().updateMany(filter, update); } }
/** * Update a single or all documents in the collection according to the specified arguments. * When upsert set to true, the new document will be inserted if there are no matches to the query filter. * * @param filter Bson filter * @param document Bson document * @param upsert a new document should be inserted if there are no matches to the query filter * @param many whether find all documents according to the query filter */ public void update(Bson filter, Bson document, boolean upsert, boolean many) { //TODO batch updating UpdateOptions options = new UpdateOptions(); if (upsert) { options.upsert(true); } if (many) { collection.updateMany(filter, document, options); } else { collection.updateOne(filter, document, options); } }
/** * Add field use_raw_configuration_schema to endpointProfile that used to support devices using * SDK version 0.9.0 */ public void transform() { //mongo MongoClient client = new MongoClient(host); MongoDatabase database = client.getDatabase(dbName); MongoCollection<Document> endpointProfile = database.getCollection("endpoint_profile"); endpointProfile.updateMany(new Document(), eq("$set", eq("use_raw_schema", false))); //cassandra Cluster cluster = Cluster.builder().addContactPoint(host).build(); Session session = cluster.connect(dbName); session.execute("ALTER TABLE ep_profile ADD use_raw_schema boolean"); session.close(); cluster.close(); } }
Object id = document.get("_id"); Long schemaId = parseLong((String) document.get("notification_schema_id")); notification.updateMany( Filters.eq("_id", id), Filters.eq("$set", Filters.eq("notification_schema_id", schemaId + idShift)) Object id = document.get("_id"); Long schemaId = parseLong((String) document.get("notification.notification_schema_id")); notification.updateMany( Filters.eq("_id", id), Filters.eq("$set", Filters.eq(
} else { if (multi) { return collection.updateMany(queryObj, updateObj, opts); } else { return collection.updateOne(queryObj, updateObj, opts);
@Override public long update(Map<String, Object> query, Map<String, Object> update) { UpdateResult updateResult = collection.updateMany(new Document(query), new Document(update)); return updateResult.wasAcknowledged() ? updateResult.getModifiedCount() : -updateResult.getModifiedCount(); }
} else { if (multi) { return collection.updateMany(queryObj, updateObj, opts); } else { return collection.updateOne(queryObj, updateObj, opts);
private void migrateGeneralScriptFunction(GlobalContext context) { logger.info("Searching for keywords of type 'Script' to be migrated..."); com.mongodb.client.MongoCollection<Document> functions = context.getMongoClientSession().getMongoDatabase().getCollection("functions"); Document filter = new Document("type", "step.plugins.functions.types.GeneralScriptFunction"); Document replacement = new Document("$set", new Document("type", "step.plugins.java.GeneralScriptFunction")); UpdateResult result = functions.updateMany(filter, replacement); logger.info("Migrated "+result.getModifiedCount()+" artefacts of type 'step.plugins.functions.types.GeneralScriptFunction'"); }
private void changeStage( IMongoSerializer.Stage stage, IMongoSerializer.Stage newStage) { Document newDocument = new Document("$set", new Document( IMongoSerializer.FIELD_STAGE, newStage.name())); // Batch update collRefs.updateMany( eq(IMongoSerializer.FIELD_STAGE, stage.name()), newDocument); }
@Test public void testUpdateMulti() throws Exception { collection.insertOne(json("a: 1")); collection.insertOne(json("a: 1")); UpdateResult result = collection.updateOne(json("a: 1"), json("$set: {b: 2}")); assertThat(result.getModifiedCount()).isEqualTo(1); assertThat(collection.countDocuments(new Document("b", 2))).isEqualTo(1); result = collection.updateMany(json("a: 1"), json("$set: {b: 3}")); assertThat(result.getModifiedCount()).isEqualTo(2); assertThat(collection.countDocuments(new Document("b", 2))).isEqualTo(0); assertThat(collection.countDocuments(new Document("b", 3))).isEqualTo(2); }
public UpdateResult update(Bson query, Bson updates, boolean upsert, boolean multi) { UpdateOptions updateOptions = new UpdateOptions().upsert(upsert); if (multi) { return dbCollection.updateMany(query, updates, updateOptions); } else { return dbCollection.updateOne(query, updates, updateOptions); } }
@Test public void testUpdateWithIdInMultiReturnModifiedDocumentCount() { collection.insertMany(Arrays.asList(json("_id: 1"), json("_id: 2"))); UpdateResult result = collection.updateMany(json("_id: {$in: [1, 2]}"), json("$set: {n: 1}")); assertThat(result.getModifiedCount()).isEqualTo(2); }
@NotNull @Override public UpdateResult updateMany(@NotNull ClientSession clientSession, @NotNull Bson filter, @NotNull Bson update) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.updateMany(clientSession, filter, update); maybeThrowExceptionAfterUpdate(); return result; }
@NotNull @Override public UpdateResult updateMany(@NotNull Bson filter, @NotNull Bson update, @NotNull UpdateOptions updateOptions) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.updateMany(filter, update, updateOptions); maybeThrowExceptionAfterUpdate(); return result; }
@Test public void testUpdateInSystemNamespace() throws Exception { for (String collectionName : Arrays.asList("system.foobar", "system.namespaces")) { MongoCollection<Document> collection = getCollection(collectionName); assertMongoWriteException(() -> collection.updateMany(eq("some", "value"), set("field", "value")), 10156, "cannot update system collection"); } }
@NotNull @Override public UpdateResult updateMany(@NotNull Bson filter, @NotNull Bson update) { maybeThrowExceptionBeforeUpdate(); UpdateResult result = collection.updateMany(filter, update); maybeThrowExceptionAfterUpdate(); return result; }
@Test public void testUpdateWithIdQuery() { collection.insertMany(Arrays.asList(json("_id: 1"), json("_id: 2"))); collection.updateMany(json("_id: {$gt:1}"), json("$set: {n: 1}")); List<Document> results = toArray(collection.find()); assertThat(results).containsExactly(json("_id: 1"), json("_id: 2, n: 1")); }
@Test public void testUpsert() throws Exception { MongoCollection<Document> collection = readOnlyClient.getDatabase("testdb").getCollection("testcollection"); assertThatExceptionOfType(MongoException.class) .isThrownBy(() -> collection.updateMany(json("{}"), Updates.set("foo", "bar"), new UpdateOptions().upsert(true))) .withMessageContaining("Command failed with error 59 (CommandNotFound): 'no such command: 'update'"); }
@Test public void testUpdateWithIdInMulti() { collection.insertMany(Arrays.asList(json("_id: 1"), json("_id: 2"))); collection.updateMany(json("_id: {$in: [1, 2]}"), json("$set: {n: 1}")); List<Document> results = toArray(collection.find()); assertThat(results).containsExactly(json("_id: 1, n: 1"), json("_id: 2, n: 1")); }
@Test public void testUpsert() { UpdateResult result = collection.updateMany(json("n:'jon'"), json("$inc: {a: 1}"), new UpdateOptions().upsert(true)); assertThat(result.getModifiedCount()).isEqualTo(0); Document object = collection.find().first(); assertThat(result.getUpsertedId()).isEqualTo(new BsonObjectId(object.getObjectId("_id"))); object.remove("_id"); assertThat(object).isEqualTo(json("n: 'jon', a: 1")); result = collection.updateOne(json("_id: 17, n: 'jon'"), json("$inc: {a: 1}"), new UpdateOptions().upsert(true)); assertThat(result.getUpsertedId()).isEqualTo(new BsonInt32(17)); assertThat(collection.find(json("_id: 17")).first()).isEqualTo(json("_id: 17, n: 'jon', a: 1")); }