将当前用户的 ID 推送到 meteorjs 中集合/文档的内部数组

Push currentUser's ID to inner Array of a collection/ document in meteorjs

本文关键字:集合 文档 数组 内部 meteorjs 用户 ID      更新时间:2023-09-26
{
    "_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" : [ ]
            },
            {
                    "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"

}

大家好,我是 MeteorJs 的新手,我正在开发一个应用程序。 我想知道如何更新"喜欢"数组。我想将当前用户的 ID 推送到喜欢中,因此在前端我将显示 yes.objectsReference.likes.length 作为喜欢的数量。

如何定位点赞数组?谢谢

您可以使用服务器上的 $ 位置运算符来更新 likes 数组。假设您要将当前用户 ID 添加到嵌入的"是"文档上的 likes 数组中,标题值为"嗯,是",那么下面显示了如何在服务器上执行此操作:

Articles.update(
    {
        "_id" : "s2QBCnv6fXv5YbjAP",
        "yes.heading" : "Well, Yes"
    },
    {
        "$push": { "yes.$.likes": userId }
    }
)