我是mongoDB的新手。我在更新mongoDB集合中的记录方面遇到了一些麻烦。将元素插入MongoDB中的嵌套数组
如何添加元素到数组“喜欢”到嵌入式记录
我有一个嵌入式的收集,如:
{
"_id" : "iL9hL2hLauoSimtkM",
"title" : "Some Topic",
"followers" : [
"userID1",
"userID2",
"userID3"
],
"comments" : [
{
"comment" : "Yes Should be....",
"userId" : "a3123",
"likes" : [
"userID1",
"userID2"
]
},
{
"comment" : "No Should not be....",
"userId" : "ahh21",
"likes" : [
"userID1",
"userID2",
"userID3"
]
}
]
}
我想更新纪录
{
"_id" : "iL9hL2hLauoSimtkM",
"title" : "Some Topic",
"followers" : [
"userID1",
"userID2",
"userID3"
],
"comments" : [
{
"comment" : "Yes Should be....",
"userId" : "a3123",
"likes" : [
"userID1",
"userID2",
"userID3" // How to write query to add this element.
]
},
{
"comment" : "No Should not be....",
"userId" : "ahh21",
"likes" : [
"userID1",
"userID2",
"userID3"
]
}
]
}
请提供查询以添加评论中显示的元素。 谢谢。
谢谢您的重播,查询工作效果很好。 –
真棒!干净和简单的 –
只是想知道是否有可能用一次更新更新多个数组项目?我有一个情况,每个文档最多可以有50个更新。 –