简体   繁体   English

MongoDB更新嵌入式文档不起作用

[英]MongoDB updating embedded document isn't working

I'm trying to update embedded document, but it is not working. 我正在尝试更新嵌入式文档,但是无法正常工作。 This is what documents look like: 这是文档的样子:

{
    "_id" : ObjectId("577c71735d35de6371388efc"),
    "category" : "A",
    "title" : "Test",
    "content" : "Test",
    "tags" : "test",
    "comments" : [
        {
            "_id" : ObjectId("57811681010bd12923eda0ca"),
            "author" : "creator",
            "email" : "creator@example.com",
            "text" : "helloworld!"
        },
        {
            "_id" : ObjectId("57811b17b667676126bde94e"),
            "author" : "creator",
            "email" : "creator@example.com",
            "text" : "helloworld2!"
        }
    ],
    "createdAt" : ...,
    "updatedAt" : ...
}

you can see the comments field is embedded document that contains comments. 您可以看到注释字段是包含注释的嵌入式文档。 I want to update specific comment, so I made query like this(node.js): 我想更新特定的注释,因此我进行了如下查询(node.js):

db.update('posts', {
    _id: new ObjectID(postId),    // ID of the post
    comments: {
        $elemMatch: {
            _id: new ObjectId(commentId)
        }
    }
}, {
    $set: {
        "comments.$.author": newComment.author,
        "comments.$.email": newComment.email,
        "comments.$.text": newComment.text,
        "comments.$.updatedAt": new Date()
    }
}) ...

when I run this query, no error was shown but update wasn't applied. 当我运行此查询时,未显示任何错误,但未应用更新。 I tried this query too: 我也尝试过以下查询:

{
    _id: new ObjectId(postId),
    "comments._id": new ObjectId(commentId)
}

but not worked either. 但也没有用。 Am I missing something? 我想念什么吗? I'm using Mongo v3.2.7. 我正在使用Mongo v3.2.7。

Please try the below code. 请尝试以下代码。 I think the "ObjectId" (ie case) should be the problem. 我认为“ ObjectId”(即大小写)应该是问题所在。 Just check how you defined the object id and keep it consistent in the two places that you have used (ie posts _id and comments _id -> both places). 只需检查您如何定义对象ID,并在使用过的两个位置(即发布_id和注释_id->两个位置)保持一致即可。

ObjectID = require('mongodb').ObjectID ObjectID = require('mongodb')。ObjectID

The below code works fine for me. 下面的代码对我来说很好。 Basically, your query seems to be correct. 基本上,您的查询似乎是正确的。

var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, assert = require('assert');

var db = new Db('localhost', new Server('localhost', 27017));
db.open(function(err, db) {
    var collection = db.collection("posts");

    var postId = '577c71735d35de6371388efc';
    var commentId = '57811681010bd12923eda0ca';

    var query = {
        _id : new ObjectID(postId), 
        comments : {
            $elemMatch : {
                _id : new ObjectID(commentId)
            }
        }
    };

    collection.update(query, {
        $set : {
            "comments.$.author" : "new author",
            "comments.$.email" : "newemail@gmail.com",
            "comments.$.text" : "new email updated",
            "comments.$.updatedAt" : new Date()
        }
    }, {
        multi : false
    }, function(err, item) {        
        assert.equal(null, err);
        console.log("comments updated ..." + JSON.stringify(item));
    });

});

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

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