【问题标题】:MongoDB findOneAndUpdate Updates But Nothing ChangesMongoDB findOneAndUpdate 更新但没有任何变化
【发布时间】:2020-08-03 07:57:36
【问题描述】:

我在地图集上有一个集群。我当前的对象如下所示:

 {
   "_id": {
      "$oid":"5e9d8ccfb3d80e3824bf856c"
   },
   "user": {
      "name":"test",
      "channelGroups": [
         {
            "active": true,
            "name": "an example of test",
            "creationDate": "20 Apr 2020 at 13:35 PM",
            "summary": "an example summary",
            "channels": []
         }
      ]
   },
   "__v": {
      "$numberInt":"0"
   }
}

这是我的控制器方法:(我使用猫鼬和快递)

createANewChannel: (req, res) => {

    channelGroupModel.findOneAndUpdate({
            "user.name": "test", "user.channelGroups": {
                $elemMatch: {
                    "name": "an example of test"  // req.params.slug
                }
            }
        }, { $push: {
                    "channelGroups.channels": {
                        filter: {
                            keyword: "#example",
                            keywordType: "hashtag",
                            active: true,
                            fetchFrequency: 1,
                        },
                        tweets: []
                    }
                }
        },
        (error, success)  => {
            if (error) {
                console.log(error);
            } else {
                console.log(success);
            }
        }
    );


    res.send('ok');
},

问题: 我想根据 channelGroups 的 name 值将一个新的 channel 对象推送到 channelGroups 数组中。我的代码有效,但没有任何反应。

================================================ =======================

更新的工作版本

channelGroupModel.findOneAndUpdate(
    {
        "user.name": "test"
    },
    {
        $push: {
            "user.channelGroups.$[elem].channels": {
                filter: {
                    keyword: "#example",
                    keywordType: "hashtag",
                    active: true,
                    fetchFrequency: 1,
                },
                tweets: []
            }
        }
    },
    {
        arrayFilters: [{"elem.name": "an example of test"}]
    },
    (error, success) => {
        if (error) {
            console.log(error);
        } else {
            console.log(success);
        }
    }
)

【问题讨论】:

    标签: node.js mongodb express mongoose mongodb-query


    【解决方案1】:

    你想使用arrayFilters:

    channelGroupModel.findOneAndUpdate(
        {
            "user.name": "test"
        },
        {
            $push: {
                "channelGroups.$[elem].channels": {
                    filter: {
                        keyword: "#example",
                        keywordType: "hashtag",
                        active: true,
                        fetchFrequency: 1,
                    },
                    tweets: []
                }
            }
        },
        {
            arrayFilters: [{"elem.name": "an example of test"}]
        },
        (error, success) => {
            if (error) {
                console.log(error);
            } else {
                console.log(success);
            }
        }
    )
    

    仅供参考,mongoose 在某些版本上对arrayFilters 有一个臭名昭著的bug

    【讨论】:

    • 您好,您的代码与“user.channelGroups.$[elem].channels”一起使用。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2020-04-11
    • 2022-08-02
    • 2019-03-14
    • 1970-01-01
    • 2011-04-09
    • 2019-02-20
    • 1970-01-01
    相关资源
    最近更新 更多