【问题标题】:Does mongodb find and update one by one or find all then update allmongodb是一一查找更新还是全部查找然后全部更新
【发布时间】:2017-02-08 00:23:30
【问题描述】:

在mongodb中,更新多个文档的写操作不是原子的。如果有3条记录A,B,C需要更新。

mongodb:

查找 A,更新 A,查找 B,更新 B,查找 C,更新 C

找到 A,B,C 并保存到内存中,forEach,更新 A,B,C?

【问题讨论】:

    标签: mongodb mongodb-update


    【解决方案1】:

    Bulk Update 在 MongoDB 中受支持。

    请看这个例子

    db.mycollection.find()
    { "_id" : 1, "name" : "A", "designation" : "developer", "company" : "XYZ" }
    { "_id" : 2, "name" : "B", "designation" : "developer", "company" : "XYZ", "expe
    rience" : 2 }
    { "_id" : 3, "name" : "C", "designation" : "developer", "company" : "XYZ", "expe
    rience" : 3 }
    { "_id" : 4, "name" : "D", "designation" : "developer", "company" : "XYZ", "expe
    rience" : 5 }
    { "_id" : 5, "name" : "E", "designation" : "Manager", "company" : "XYZ", "experi
    ence" : 10 }
    { "_id" : 6, "name" : "F", "designation" : "Manager", "company" : "XYZ", "experi
    ence" : 10 }
    { "_id" : 7, "name" : "G", "designation" : "Manager", "company" : "ABC", "experi
    ence" : 10 }
    { "_id" : 8, "name" : "H", "designation" : "Developer", "company" : "ABC", "expe
    rience" : 5 }
    { "_id" : 9, "name" : "I", "designation" : "Developer", "company" : "XYZ", "expe
    rience" : 5 }
    

    我将根据以下条件对 mycollection 进行批量更新

    XYZ 公司向他们的开发人员成员提供晋升 有5年资深开发经验

    我在 Mongo shell 中的命令是

    var bulk = db.mycollection.initializeUnorderedBulkOp();
    
    bulk.find({ $and:[{company:"XYZ"}, {experience:5}] }).update({$set:{designation:"
    senior developer", comments:"Congrats you have been promoted to senior developer
    "}});
    
    bulk.execute();
    

    Mongo Shell 执行查询后我们会得到以下结果

    BulkWriteResult({
            "writeErrors" : [ ],
            "writeConcernErrors" : [ ],
            "nInserted" : 0,
            "nUpserted" : 0,
            "nMatched" : 2,
            "nModified" : 2,
            "nRemoved" : 0,
            "upserted" : [ ]
    })
    

    符合我们公司条件的两个文档:“XYZ”和经验:5,并且这些文档已更新(_id:4,_id:9)

    db.mycollection.find()
    
    { "_id" : 1, "name" : "A", "designation" : "developer", "company" : "XYZ" }
    { "_id" : 2, "name" : "B", "designation" : "developer", "company" : "XYZ", "expe
    rience" : 2 }
    { "_id" : 3, "name" : "C", "designation" : "developer", "company" : "XYZ", "expe
    rience" : 3 }
    { "_id" : 4, "name" : "D", "designation" : "senior developer", "company" : "XYZ"
    , "experience" : 5, "comments" : "Congrats you have been promoted to senior deve
    loper" }
    { "_id" : 5, "name" : "E", "designation" : "Manager", "company" : "XYZ", "experi
    ence" : 10 }
    { "_id" : 6, "name" : "F", "designation" : "Manager", "company" : "XYZ", "experi
    ence" : 10 }
    { "_id" : 7, "name" : "G", "designation" : "Manager", "company" : "ABC", "experi
    ence" : 10 }
    { "_id" : 8, "name" : "H", "designation" : "Developer", "company" : "ABC", "expe
    rience" : 5 }
    { "_id" : 9, "name" : "I", "designation" : "senior developer", "company" : "XYZ"
    , "experience" : 5, "comments" : "Congrats you have been promoted to senior deve
    loper" }
    

    其他相关的有趣参考资料:

    What's the difference between findAndModify and update in MongoDB?

    https://docs.mongodb.com/manual/reference/method/Bulk.find.update/

    https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/

    https://docs.mongodb.com/v3.2/reference/method/db.collection.findOneAndUpdate/

    希望对你有帮助!!

    【讨论】:

      猜你喜欢
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2016-03-29
      • 2020-08-09
      • 1970-01-01
      相关资源
      最近更新 更多