简体   繁体   English

MongoDb中的多次更新问题

[英]Problems with multiple update in MongoDb

I have mongo structure like this: 我有这样的mongo结构:

{
        "_id" : ObjectId("51596b7e469b9c3816000001"),
        "company" : {
                "_id" : "ade2fd0ec9b8b5e9152e0155",
                "title" : "LO3426546457"
        },
}
{
        "_id" : ObjectId("51596cef469b9c3816000008"),
        "company" : {
                "_id" : "ade2fd0ec9b8b5e9152e0155",
                "title" : "LO3426546457"
        },
}
{
        "_id" : ObjectId("51596cc3469b9c3816000007"),
        "company" : {
                "_id" : "ade2fd0ec9b8b5e9152e0155",
                "title" : "LO3426546457"
        }        
}

And I want to change all 'title' fields for objects with a specific '_id'. 我想更改具有特定“ _id”的对象的所有“标题”字段。 I do like this: 我喜欢这样:

Collections.UsersCollection.update({
    'company._id': 'ade2fd0ec9b8b5e9152e0155'
}, {
    $set: {
        'company': { _id: 'ade2fd0ec9b8b5e9152e0155', title: 'blablabla' }
        // I also tried: 'company.title': 'blablabla'
    }
}, false, true);

And after execution that code in Node.js (I use node-mongodb-native), mongo updates only one document. 在Node.js(我使用node-mongodb-native)中执行该代码后,mongo 更新一个文档。

But if I do that command at Mongo Shell (mongo.exe), everything works fine and updates all documents. 但是,如果我在Mongo Shell(mongo.exe)上执行该命令,则一切正常,并更新所有文档。

What's the problem? 有什么问题?

By default mongo updates only a single document. 默认情况下,mongo仅更新单个文档。 http://docs.mongodb.org/manual/reference/method/db.collection.update/ http://docs.mongodb.org/manual/reference/method/db.collection.update/

Use multi option for multiple document update. 使用多重选项更新多个文档。

默认情况下,collection.update()仅更新单个文档,如果您需要更新多个文档,则必须在回调函数之前添加{multi:1}参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM