首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用upsert使用updateone批量更新mongo

无法使用upsert使用updateone批量更新mongo
EN

Stack Overflow用户
提问于 2016-12-26 02:55:53
回答 0查看 449关注 0票数 2

我正面临着一个问题。我正在尝试更新如果已经存在,否则插入如果不存在。我使用的是spring-mongo驱动程序的批量API。

代码语言:javascript
复制
DBCollection dbCollection = mongoTemplate.getCollection("supcInfo");
        BulkWriteOperation bulkWriteOperation = dbCollection.initializeUnorderedBulkOperation();
BulkUpdateRequestBuilder builder = bulkWriteOperation.find(new BasicDBObject("_id", supcInfo.getSupc())).upsert();
BasicDBObject dbObject = new BasicDBObject("$set",new BasicDBObject("_id", supcInfo.getSupc()));
         dbObject = dbObject.append("$set",new BasicDBObject("pogId", supcInfo.getPogId()));
         dbObject = dbObject.append("$set",new BasicDBObject("mrp", supcInfo.getMrp()));
         dbObject = dbObject.append("$set",new BasicDBObject("price", supcInfo.getPrice()));
         dbObject = dbObject.append("$set",new BasicDBObject("primarySellerCode", supcInfo.getPrimarySellerCode()));
         dbObject =  dbObject.append("$set",new BasicDBObject("camsEnabled", supcInfo.isCamsEnabled()));
         dbObject =  dbObject.append("$set",new BasicDBObject("availability", supcInfo.getAvailability()));
         dbObject = dbObject.append("$set",new BasicDBObject("updateTs", supcInfo.getUpdateTs()));
builder.updateOne(dbObject);
bulkWriteOperation.execute();

但并不是所有字段都在更新。谁能告诉我原因和replaceOne工作很好,但它将重新创建索引,如果有任何。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41323737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档