在MongoDB中嵌套数组文档中第n次更新文件 [英] update nth document in a nested array document in mongodb
本文介绍了在MongoDB中嵌套数组文档中第n次更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在蒙戈DB另一个文档里的数组更新文档
{
_id:的ObjectId(51cff693d342704b5047e6d8),
作者:测试,
身:sdfkj dsfhk asdfjad
注释 : [
{
作者:测试,
身:sdfkjdj \\ r \\ nasdjgkfdfj
电子邮件:test@tes.com
},
{
作者:HOLA
身:SDFL \\ r \\ nhola \\ r \\ nwork这里
}
]
日期:ISODate(2013-06-30T09:12:51.629Z),
永久:jaiho
标签:
jaiho
]
头衔:JAiHo
}
Q1)评论阵列的第0个元素的更新电子邮件
db.posts.update({永久:哈哈},{$集:{comments.0.email:1}})
这不抛出任何异常,但不更新任何内容,以及
Q2)添加一个字段的意见阵列number_likes第n个元素
db.posts.update({永久:哈哈},{$ INC:{comments.0.num_likes:1}})
也不行。我失去了一些东西在这里?
解决方案
Q1:如果您使用永久链接jaiho更新,而不是'哈哈',它肯定会更新的电子邮件;
> db.posts.update({永久:jaiho},{$集合:{comments.0.email:1}})
> db.posts.find()
...,电子邮件:1},...
Q2:同去的这包括:
> db.posts.update({永久:jaiho},{$增量:{comments.0.num_likes:1}})
> db.posts.find()
...,num_likes:1},...
I need to update a document in an array inside another document in Mongo DB
{ "_id" : ObjectId("51cff693d342704b5047e6d8"), "author" : "test", "body" : "sdfkj dsfhk asdfjad ", "comments" : [ { "author" : "test", "body" : "sdfkjdj\r\nasdjgkfdfj", "email" : "test@tes.com" }, { "author" : "hola", "body" : "sdfl\r\nhola \r\nwork here" } ], "date" : ISODate("2013-06-30T09:12:51.629Z"), "permalink" : "jaiho", "tags" : [ "jaiho" ], "title" : "JAiHo" } Q1) Update email of 0th element of comments array db.posts.update({"permalink" : "haha"},{$set:{"comments.0.email":1}}) This doesn't throw any exception but doesn't update anything as well Q2) Add a field on nth element of comments array number_likes db.posts.update({"permalink" : "haha"},{$inc:{"comments.0.num_likes":1}}) Doesn't work either. Am I missing something here?
解决方案
Q1: If you update with permalink 'jaiho' instead of 'haha', it most certainly updates the email;
> db.posts.update({"permalink" : "jaiho"},{$set:{"comments.0.email":1}})
> db.posts.find()
..., "email" : 1 },...
Q2: Same goes for this include;
> db.posts.update({"permalink" : "jaiho"},{$inc:{"comments.0.num_likes":1}})
> db.posts.find()
..., "num_likes" : 1 },...
这篇关于在MongoDB中嵌套数组文档中第n次更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文