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

Updating a nested array in a collection with it's index

本文关键字:数组 索引 嵌套 更新 集合      更新时间:2023-09-26

我是MeteorJs的新手,我很喜欢它。但我是,我正在开发一款流星Js应用程序,它必须有意见(是或否)并投票。该结构使得问题(主帖子)是评论的父级,评论是父级点赞,即问题(对象)>是或否(数组)>点赞(数组)所以,我有这个帖子的收集结构(在这种情况下的问题)

{
    "_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"

}

我想要的是:写一个助手,当点击点赞按钮时,将loggedIn用户的ID推入点赞数组。我写了这个助手

 "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"}}
    );        
}

然后它工作,但它只更新第一个"赞"数组。

所以,这是我的问题。如何更新问题>是/否>点赞数组中的每个点赞。感谢

我认为你应该在你的yes数组中生成额外的id

"yes" : [
         {
           id : Random.id(),
           heading : "..."
           ...

然后使用位置$operator,因此:

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