使用索引更新集合中的嵌套数组

时间:2016-05-17 15:46:14

标签: javascript mongodb meteor

我是MeteorJs的新手,我很喜欢它。但我,我正在制作一个meteorJs应用程序,必须与意见(是或否)和投票。结构是这样的,问题(主要帖子)是评论的父母,评论是父母喜欢的,即问题(对象)>是或否(数组)>喜欢(数组) 所以,我有这个帖子的收集结构(在这种情况下的问题)

{
    "_id" : "s2QBCnv6fXv5YbjAP",
    "question" : "Is this real change?",
    "createdAt" : ISODate("2016-05-13T21:05:23.381Z"),
    "yes" : [
            {
                    "heading" : "Yes It is",
                    "body" : "I think this government knows what they are doing. That's why there has not been any form of protest",
                    "email" : "I think this government knows what they are doing. That's why there has not been any form of protest",
                    "createdAt" : ISODate("2016-05-13T21:08:25.119Z"),
                    "replies" : [ ],
                    "likes" : [
                            "sdfsd6sd556shsdbdjs88s",
                            "sdfsd6sd556shsdbdjkhj88s",
                            "the_use_id",
                            "the_use_id",
                            "the_use_id"
                    ]
            },
            {
                    "heading" : "Well, Yes",
                    "body" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
                    "email" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
                    "createdAt" : ISODate("2016-05-13T21:10:47.123Z"),
                    "replies" : [ ],
                    "likes" : [ ]
            }
    ],
    "no" : [
            {
                    "heading" : "Not at All",
                    "body" : "This is not the change I wanted. This is waste of four years and I amm waiting to see the promised change",
                    "email" : "kenshin@kay.com",
                    "createdAt" : ISODate("2016-05-13T21:12:58.977Z"),
                    "replies" : [ ],
                    "likes" : [ ]
            }
    ],
    "author" : "admin",
    "image" : "/cfs/files/QuestionImages/DzdpK6NdurZMTwAse"

}

我想要的是:当点击like按钮时写一个帮助器将loggedIn用户的ID推送到likes数组我写了这个帮助器

 "click #like_yes_comment": function(event){
    event.preventDefault();

    var questionId = Template.parentData(1)._id;
    Questions.update(
        {_id: questionId, , 'yes.heading':this.heading}, 
        {$push: {'yes.0.likes': "the_use_id"}}
    );        

}

但是我收到了这个错误

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403]

然后我重新编辑它看起来像这样

"click #like_yes_comment": function(event){
    event.preventDefault();

    var questionId = Template.parentData(1)._id;
    Questions.update(
        {_id: questionId}, 
        {$push: {'yes.0.likes': "the_use_id"}}
    );        

}

然后它可以工作,但它只更新第一个“喜欢”数组。

所以,这是我的问题。如何在问题>是/否>喜欢的数组中更新每个。谢谢

1 个答案:

答案 0 :(得分:0)

我认为您应该在数组

中生成额外的ID
"yes" : [
         {
           id : Random.id(),
           heading : "..."
           ...

然后使用位置$运算符,所以:

Questions.update(
    {_id: questionId, "yes.id": yesId}, 
    {$push: {'yes.$.likes': userId}}
);