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

The following are Jave code examples for showing how to use updateMany() of the com.mongodb.client.MongoCollection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mongodb-crud   File: UpdateDocumentsImpl.java   View Source Code Vote up 6 votes
/**
 * This method update all the matches document
 */
@Override
public void updateManyDocument() {
	MongoDatabase db = null;
	MongoCollection collection = null;
	Bson filter = null;
	Bson query = null;
	try {
		db = client.getDatabase(mongo.getDataBase());
		collection = db.getCollection(mongo.getSampleCollection());
		filter = eq("name", "Sundar");
		query = combine(set("age", 23), set("gender", "Male"));
		UpdateResult result = collection.updateMany(filter, query);
		log.info("UpdateMany Status : " + result.wasAcknowledged());
		log.info("No of Record Modified : " + result.getModifiedCount());
	} catch (MongoException e) {
		log.error("Exception occurred while update Many Document : " + e, e);
	}
}
 
Example 2
Project: dooo   File: MongodbDataAccess.java   View Source Code Vote up 6 votes
/**
 * 新增或者更新
 *
 * @param collectionName 集合名
 * @param query          查询条件
 * @param descData       目标数据
 * @return
 */
public boolean upsert(String collectionName, MongodbQuery query, Map<String, Object> descData) {
    MongoCollection collection = sMongoDatabase.getCollection(collectionName);
    UpdateOptions options = new UpdateOptions();
    options.upsert(true);
    BasicDBObject updateSetValue = new BasicDBObject("$set", descData);
    UpdateResult updateResult = collection.updateMany(query.getQuery(), updateSetValue, options);
    return updateResult.getUpsertedId() != null ||
            (updateResult.getMatchedCount() > 0 && updateResult.getModifiedCount() > 0);
}
 
Example 3
Project: MooProject   File: DatabaseConnection.java   View Source Code Vote up 4 votes
public long upsertManySync(MongoCollection<Document> collection, Bson filter, Document document) {
    UpdateResult result = collection.updateMany(filter, document, new UpdateOptions().upsert(true));
    return result.getUpsertedId() == null ? result.getModifiedCount() : 1;
}
 
Example 4
Project: dooo   File: MongodbDataAccess.java   View Source Code Vote up 3 votes
/**
 * 更新符合条件的所有记录
 *
 * @param collectionName 集合名
 * @param query          查询条件
 * @param update         更新设置
 * @return
 */
public boolean updateMany(String collectionName, MongodbQuery query, MongodbUpdates update) {
    MongoCollection collection = sMongoDatabase.getCollection(collectionName);
    UpdateResult updateResult = collection.updateMany(query.getQuery(), update.getUpdates());
    return updateResult.getUpsertedId() != null ||
            (updateResult.getMatchedCount() > 0 && updateResult.getModifiedCount() > 0);
}
 
Example 5
Project: dooo   File: MongodbDataAccess.java   View Source Code Vote up 3 votes
/**
 * 移除指定字段
 *
 * @param collectionName
 * @param query
 * @param fieldName
 */
public void removeField(String collectionName, MongodbQuery query, String fieldName) {
    MongoCollection collection = sMongoDatabase.getCollection(collectionName);
    UpdateOptions options = new UpdateOptions();
    options.upsert(true);
    collection.updateMany(query.getQuery(), MongodbUpdates.unset(fieldName).getUpdates(), options);
}