[英]Upsert of collection in MongoDB, using Java.
Can anybody help me with this "some magic" that I am looking for? 有人可以帮我解决我正在寻找的“某种魔术”吗?
I am using Java, Heroku, mLab:MongoDB tech stack. 我正在使用Java,Heroku,mLab:MongoDB技术堆栈。
Assuming the in memory
database is a collection and each document has the id, you can use insertAll
method of MongoTemplate
to dump the whole collection. 假设
in memory
数据库是一个集合,每个文档都有的ID,你可以使用insertAll
的方法MongoTemplate
倾倒整个集合。 It will keep the unchanged documents as is and update the others. 它将保留未更改的文档,并更新其他文档。
As far as upsert
is concerned, you can create a Query
and use upsert
method of MongoTemplate
, eg: 就
upsert
而言,您可以创建Query
并使用MongoTemplate
upsert
方法,例如:
Query query = new Query(Criteria.where("_id").is(my_id));
mongoTemplate.upsert(query, document, Document.class, "collection_name");
Here's the javadoc of insertAll and upsert methods. 这是insertAll和upsert方法的javadoc。
Update 更新资料
If you are using core mongo-java-driver then you can have a look at this and this SO answers to do the same operations. 如果你正在使用的核心蒙戈- Java的应用程序,那么你可以看看这个和这个 SO答案做同样的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.