Quantcast
Channel: MongoDB updating embedded document isn't working - Stack Overflow
Viewing all articles
Browse latest Browse all 2

MongoDB updating embedded document isn't working

$
0
0

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):

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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images