如果不存在则更新或创建 Mongo 3.0.4
Update or create if not exist Mongo 3.0.4
我有以下数据结构的集合
{
"Shop": "123",
"date": "28-05-2015",
"Points": {
"a": "1",
"b": "2",
"c": "3"
}
}
这里的 shop 和 date 有唯一的索引,除了 _id 没有其他 id,
因此,如果我只想更新 b 的值并且记录匹配(基于帐户和日期)不存在,那么应该创建与 b 的值具有相同结构的新记录。
最重要的部分是它的批量操作意味着我需要 create/update 每天为所有商店记录,这些记录将每天为每个商店创建。因此,如果记录不存在,则更新点而不是创建它,否则更新现有记录。我正在使用 spring 。
我尝试使用 update with upsert true 但它不是更新而是创建一个只有更新值的新记录。用给定的更新点更新多个商店的最佳方法是什么。
//search a document that doesn't exist
Query query = new Query();
query.addCriteria(Criteria.where("points.b").is("some value"));
Update update = new Update();
update.set("b", some value);
update.set..... //set all needed fields else they go as null
mongoOperation.upsert(query, update, Shop.class);
Shop shopTestObj = mongoOperation.findOne(query, Shop.class);
System.out.println("shopTestObj - " + shopTestObj );
在批量操作的情况下
// Sample code
com.mongodb.DBCollection collection = db.getCollection("shop");
// Get BulkWriteOperation by accessing the mongodb com.mongodb.DBCollection class on mycol //Collection
BulkWriteOperation bulkWriteOperation= collection.initializeUnorderedBulkOperation();
//perform the upsert operation in the loop to add objects for bulk execution
for (int i=0;i<100;i++)
{
// get a bulkWriteRequestBuilder by issuing find on the shop with _id
BulkWriteRequestBuilder bulkWriteRequestBuilder=bulkWriteOperation.find(new BasicDBObject('_id',Integer.valueOf(i)));
// get hold of upsert operation from bulkWriteRequestBuilder
BulkUpdateRequestBuilder updateReq= bulkWriteRequestBuilder?.upsert()
updateReq. replaceOne (new BasicDBObject("_id",Integer.valueOf(i)).append("points.b", "some value"));
}
// execute bulk operation on shop collection
BulkWriteResult result=bulkWriteOperation.execute();
我有以下数据结构的集合
{
"Shop": "123",
"date": "28-05-2015",
"Points": {
"a": "1",
"b": "2",
"c": "3"
}
}
这里的 shop 和 date 有唯一的索引,除了 _id 没有其他 id, 因此,如果我只想更新 b 的值并且记录匹配(基于帐户和日期)不存在,那么应该创建与 b 的值具有相同结构的新记录。 最重要的部分是它的批量操作意味着我需要 create/update 每天为所有商店记录,这些记录将每天为每个商店创建。因此,如果记录不存在,则更新点而不是创建它,否则更新现有记录。我正在使用 spring 。 我尝试使用 update with upsert true 但它不是更新而是创建一个只有更新值的新记录。用给定的更新点更新多个商店的最佳方法是什么。
//search a document that doesn't exist
Query query = new Query();
query.addCriteria(Criteria.where("points.b").is("some value"));
Update update = new Update();
update.set("b", some value);
update.set..... //set all needed fields else they go as null
mongoOperation.upsert(query, update, Shop.class);
Shop shopTestObj = mongoOperation.findOne(query, Shop.class);
System.out.println("shopTestObj - " + shopTestObj );
在批量操作的情况下
// Sample code
com.mongodb.DBCollection collection = db.getCollection("shop");
// Get BulkWriteOperation by accessing the mongodb com.mongodb.DBCollection class on mycol //Collection
BulkWriteOperation bulkWriteOperation= collection.initializeUnorderedBulkOperation();
//perform the upsert operation in the loop to add objects for bulk execution
for (int i=0;i<100;i++)
{
// get a bulkWriteRequestBuilder by issuing find on the shop with _id
BulkWriteRequestBuilder bulkWriteRequestBuilder=bulkWriteOperation.find(new BasicDBObject('_id',Integer.valueOf(i)));
// get hold of upsert operation from bulkWriteRequestBuilder
BulkUpdateRequestBuilder updateReq= bulkWriteRequestBuilder?.upsert()
updateReq. replaceOne (new BasicDBObject("_id",Integer.valueOf(i)).append("points.b", "some value"));
}
// execute bulk operation on shop collection
BulkWriteResult result=bulkWriteOperation.execute();