Ich versuche, das eingebettete Dokument zu aktualisieren, aber es funktioniert nicht. Dies ist, was Dokumente wie folgt aussehen:MongoDB aktualisierten eingebetteten Dokument funktioniert nicht
{
"_id" : ObjectId("577c71735d35de6371388efc"),
"category" : "A",
"title" : "Test",
"content" : "Test",
"tags" : "test",
"comments" : [
{
"_id" : ObjectId("57811681010bd12923eda0ca"),
"author" : "creator",
"email" : "[email protected]",
"text" : "helloworld!"
},
{
"_id" : ObjectId("57811b17b667676126bde94e"),
"author" : "creator",
"email" : "[email protected]",
"text" : "helloworld2!"
}
],
"createdAt" : ...,
"updatedAt" : ...
}
Sie die Kommentare sehen Feld Dokument eingebettet ist, die Kommentare enthält. Ich möchte Kommentar verfasst aktualisieren, so dass ich Abfrage wie folgt (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()
}
}) ...
, wenn ich diese Abfrage ausführen, wurde kein Fehler angezeigt, aber Update wurde nicht angewendet. Ich habe diese Abfrage auch versucht:
{
_id: new ObjectId(postId),
"comments._id": new ObjectId(commentId)
}
aber auch nicht funktioniert. Fehle ich etwas? Ich benutze Mongo v3.2.7.
Warum aktualisieren Sie das Kommentarmodell nicht direkt? –
@MedetTleukabiluly wie? – modernator