Java Code Examples for com.mongodb.client.MongoCollection#updateMany()

The following examples show how to use com.mongodb.client.MongoCollection#updateMany() . 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: RenderDao.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public void updateZForSection(final StackId stackId,
                              final String sectionId,
                              final Double z)
        throws IllegalArgumentException, IllegalStateException {

    MongoUtil.validateRequiredParameter("stackId", stackId);
    MongoUtil.validateRequiredParameter("sectionId", sectionId);
    MongoUtil.validateRequiredParameter("z", z);

    final MongoCollection<Document> tileCollection = getTileCollection(stackId);
    final Document query = new Document("layout.sectionId", sectionId);
    final Document update = new Document("$set", new Document("z", z));

    final UpdateResult result = tileCollection.updateMany(query, update);

    LOG.debug("updateZForSection: updated {} tile specs with {}.update({},{})",
              result.getModifiedCount(), MongoUtil.fullName(tileCollection), query.toJson(), update.toJson());
}
 
Example 2
Source File: RenderDao.java    From render with GNU General Public License v2.0 6 votes vote down vote up
public void updateZForTiles(final StackId stackId,
                            final Double z,
                            final List<String> tileIds)
        throws IllegalArgumentException, IllegalStateException {

    MongoUtil.validateRequiredParameter("stackId", stackId);
    MongoUtil.validateRequiredParameter("z", z);
    MongoUtil.validateRequiredParameter("tileIds", tileIds);

    final MongoCollection<Document> tileCollection = getTileCollection(stackId);
    final Document query = new Document("tileId", new Document("$in", tileIds));
    final Document update = new Document("$set", new Document("z", z));

    final UpdateResult result = tileCollection.updateMany(query, update);

    final String shortQueryForLog = "{ 'tileId': { '$in': [ " + tileIds.size() + " tile ids ... ] } }";
    LOG.debug("updateZForTiles: updated {} tile specs with {}.update({},{})",
              result.getModifiedCount(), MongoUtil.fullName(tileCollection), shortQueryForLog, update.toJson());
}
 
Example 3
Source File: Update.java    From morphia with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the update
 *
 * @param options the options to apply
 * @return the results
 */
public UpdateResult execute(final UpdateOptions options) {
    Document updateOperations = toDocument();
    final Document queryObject = getQuery().toDocument();

    ClientSession session = getDatastore().findSession(options);
    MongoCollection<T> mongoCollection = options.prepare(getCollection());
    if (options.isMulti()) {
        return session == null ? mongoCollection.updateMany(queryObject, updateOperations, options)
                               : mongoCollection.updateMany(session, queryObject, updateOperations, options);

    } else {
        return session == null ? mongoCollection.updateOne(queryObject, updateOperations, options)
                               : mongoCollection.updateOne(session, queryObject, updateOperations, options);
    }
}
 
Example 4
Source File: MongodbClientOpsDemo.java    From xian with Apache License 2.0 5 votes vote down vote up
private static void update() {
    MongoCollection<Person> collection = Mongo.getCollection("dianping-collection", Person.class);
    //更新一条document
    collection.updateOne(eq("name", "张三"), combine(set("age", 23), set("name", "Ada Lovelace")));

    // 更新多条document
    UpdateResult updateResult = collection.updateMany(not(eq("zip", null)), set("zip", null));
    System.out.println(updateResult.getModifiedCount());

    // 替换collection(理论上object id是不会被替换的)
    updateResult = collection.replaceOne(eq("name", "张三"), new Person("张三", 20, new Address("香柏广场", "广州", "10086")));
    System.out.println(updateResult.getModifiedCount());
}
 
Example 5
Source File: MongoImpl.java    From tephra with MIT License 5 votes vote down vote up
@Override
public void update(String key, String collection, JSONObject set, JSONObject where) {
    MongoCollection<Document> mc = getCollection(key, collection);
    if (mc == null)
        return;

    mc.updateMany(toDocument(where), new Document("$set", toDocument(set)));
}
 
Example 6
Source File: MongoCollectionUpdate.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
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 data = getNamedParam( argStruct, "data", null );
	if ( data == null )
		throwException( _session, "please specify data to update" );

	cfData query = getNamedParam( argStruct, "query", null );
	if ( query == null )
		throwException( _session, "please specify query to update" );

	boolean upsert = getNamedBooleanParam( argStruct, "upsert", false );
	boolean multi = getNamedBooleanParam( argStruct, "multi", false );

	try {

		MongoCollection<Document> col = db.getCollection( collection );
		Document qry = getDocument( query );
		Document dat = getDocument( data );
		long start = System.currentTimeMillis();

		if ( multi )
			col.updateMany( qry, dat, new UpdateOptions().upsert( upsert ) );
		else
			col.updateOne( qry, dat, new UpdateOptions().upsert( upsert ) );

		_session.getDebugRecorder().execMongo( col, "update", qry, System.currentTimeMillis() - start );

		return cfBooleanData.TRUE;

	} catch ( MongoException me ) {
		throwException( _session, me.getMessage() );
		return null;
	}
}
 
Example 7
Source File: MongoConnectionWrapper.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncUpdate(
    String database,
    String collection,
    BsonDocument selector,
    BsonDocument update,
    boolean upsert,
    boolean multiUpdate) throws MongoException {

  try {
    UpdateOptions updateOptions = new UpdateOptions().upsert(
        upsert
    );

    MongoCollection<org.bson.BsonDocument> mongoCollection =
        owner.getDriverClient()
            .getDatabase(database)
            .getCollection(collection, org.bson.BsonDocument.class);
    org.bson.BsonDocument translatedUpdate =
        MongoBsonTranslator.translate(update);
    if (multiUpdate) {
      mongoCollection.updateMany(translatedUpdate, translatedUpdate, updateOptions);
    } else {
      mongoCollection.updateOne(translatedUpdate, translatedUpdate, updateOptions);
    }
  } catch (com.mongodb.MongoException ex) { //a general Mongo driver exception
    if (ErrorCode.isErrorCode(ex.getCode())) {
      throw toMongoException(ex);
    } else {
      throw toRuntimeMongoException(ex);
    }
  }
}