What is the correct method to update a document while using Mongo Cursor?

丶灬走出姿态 提交于 2020-05-15 05:10:50

问题


I am trying to update a document in Mongo DB using cursor. My Mongo DB Java driver version is 3.3.0. Following is my code snippet.

MongoCollection<Document> collection = mongoDb.getCollection("customer");
MongoCursor<Document> cursor = collection.find().iterator();
try{
    while(cursor.hasNext()){
        Document oldDoc = cursor.next();
        //created new Document newDoc
        collection.replaceOne(oldDoc, newDoc);
    }
}catch(Exception e){
    e.printStackTrace();
}

Though this way, I can update the document, I think this is not the efficient way because here collection is being searched 2 times. I want to update the old document with some values and later on want to save it using some methods like collection.update(oldDoc) or collection.save(oldDoc) without creating a new document. I searched and came across the following post.

Java, MongoDB: How to update every object while iterating a huge collection?

This is exactly what I want, but I don't find save() method in the new API. So I have 3 questions here.

  1. What is equivalent of save() method in Mongo DB Java driver 3.3.0 API through which I can update or save a document while iterating a cursor?
  2. Is there anyway by which I can update the existing document without creating a new document in Mongo DB Java driver 3.3.0 API?
  3. Above link shows save() was part of Mongo DB Java driver API earlier. Any expert answer, why it got dropped from API?

回答1:


Don't worry about the additional search, the replaceOne method's first parameter is a filter. Using the old doc as filter will use the _id property of the document which should be relatively fast. So basically, the way you do it, is correct. But bear in mind that the operation is non-transactional, which means the update may affect the cursor's result set. So it could happen, that the updated document will be stored at a positions the cursor has not yet passed, resulting in the document being processed twice.

Regarding your questions.

  1. The semantics of save method in mongo shell is to update or insert the document if not yet existing (aka upsert). The closest thing to that on the MongoCollection is updateOne() with an UpdateOptions upsert set to true. That's basically what the save command does. But replaceOne leads to the same result, if the replacement document is a modified version of the original one.

  2. Why worrying about object creation? In java it comes at next to no cost and don't worry about GC or Memory consumption as those shortlived objects are GC'ed pretty fast. If you want to have code more concise you may create a dedicated filter object with just the _id of the oldDoc, modify the oldDoc and use it instead of the new Doc.

For example:

UpdateOptions upsert = new UpdateOptions().upsert(true);
Bson idFilter = Filters.eq("_id", oldDoc.getObjectId("_id"));
//modify the oldDoc
oldDoc. ...
collection.replaceOne(idFilter, oldDoc, upsert);


来源:https://stackoverflow.com/questions/41768431/what-is-the-correct-method-to-update-a-document-while-using-mongo-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!