[英]Mongodb - How to update a nested Array
I have to update an Array nested in another Array. 我必须更新嵌套在另一个数组中的一个数组。 Is it possible to do that with the $addtoset operator?
使用$ addtoset运算符可以做到这一点吗?
Here is my document. 这是我的文件。 I have a library wich has an Array of authors who have an Array of Books.
我有一个图书馆,图书馆里有很多作家,还有很多书。
In this exemple, i'd like to add a new category to the book id 1 of the author id 1 在这个示例中,我想为作者ID 1的书ID 1添加一个新类别
How can i do that ? 我怎样才能做到这一点 ?
{
"id" : NumberLong(666),
"library" : [
{
"id" : NumberLong(8888),
"author" : [
{
"id" : NumberLong(1),
"books" : [
{
"id":1,
"title" : "plop",
"category" : ["horror"],
"isbn" : 12345
},
{
"id":2,
"title" : "plup",
"category" : ["comics"],
"isbn" : 6789
}
]
},
{
"id" : NumberLong(2),
"books" : [
{
"id":3,
"title" : "blop",
"category" : ["horror"],
"isbn" : 96325
},
{
"id":4,
"title" : "blup",
"category" : ["comics"],
"isbn" : 74125
}
]
}
]
}
]
}
I tried this : 我尝试了这个:
db.library.update(
{"id":666,"author.id":1,"books.id":1},
{"$addToSet": {"author.$.books.category": "humour" }}
)
But it doesn't works 但这行不通
How does it works ? 它是如何运作的?
thanks a lot 非常感谢
db.library.update(
{"id":666,"library.author.id":1,"library.author.books.id":1},
{"$addToSet": {"library.0.author.0.books.$.category": "Book"}}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.