【问题标题】:ArrayFilter is failing to match any elementsArrayFilter 无法匹配任何元素
【发布时间】:2021-02-13 01:42:03
【问题描述】:

我正在开发一个简单的网络论坛系统,并且在尝试为用户提供一种将消息发布到线程的方法时遇到了这个问题。每个线程是组文档中的一个数组内的一个文档,然后每个线程文档内是一个消息数组,该数组应该包含消息文档。

我的查询是:

 db.grops.update({},{$push:{"threads.$[elem].messages":"hello"}},{arrayFilters:[{"elem._id":ObjectId("602645582fa6cb13f8449057")}]})

我的数据库组表是:

{
    "_id" : ObjectId("60264286f5440e114c189212"),
    "members" : [ ],
    "name" : "Computer Science London",
    "location" : {
            "coordinates" : [
                    51.503391,
                    -0.12763
            ],
            "_id" : ObjectId("60264286f5440e114c189213"),
            "type" : "Point"
    },
    "description" : "A group for computing students to learn, talk and share!",
    "pictureURL" : "/images/computer.jpg",
    "threads" : [
            {
                    "_id" : ObjectId("602645582fa6cb13f8449057"),
                    "title" : "What is a lambda function",
                    "openingPost" : "I am reading this textbook and it mentions this thing called a lambda function, but it doesnt seem to outline what it is. Can someone help me understand?",
                    "postedBy" : "ElBarto88",
                    "updatedAt" : ISODate("2021-02-12T09:07:36.833Z"),
                    "createdAt" : ISODate("2021-02-12T09:07:36.833Z"),
                    "messages" : [ ]
            },
            {
                    "_id" : ObjectId("602645af2fa6cb13f8449058"),
                    "title" : "How do i install linux?",
                    "openingPost" : "This server assignment requires i have linux installed, how do install it?",
                    "postedBy" : "ElBarto88",
                    "updatedAt" : ISODate("2021-02-12T09:09:03.123Z"),
                    "createdAt" : ISODate("2021-02-12T09:09:03.123Z"),
                    "messages" : [ ]
            }
    ],
    "createdAt" : ISODate("2021-02-12T08:55:34.273Z"),
    "updatedAt" : ISODate("2021-02-12T09:09:03.123Z"),
    "__v" : 0
}

我似乎无法通过任何查询来获得任何匹配,然后将“hello”推送到子数组。我也尝试通过{"elem.postedBy:"ElBarto88"} 进行匹配,但无济于事。对于所有涉及 arrayFilters 的查询,我会返回:

WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

目前我只是通过将查询直接发布到 mongo shell 进行测试(我在主应用程序中使用 mongoose)。我的shell版本是:v4.4.2,我的服务器版本(MongoDB atlas集群)是:v4.4.3

【问题讨论】:

    标签: javascript mongodb mongoose


    【解决方案1】:

    我相信你可以做到:

    db.grops.update({"threads.$._id":ObjectId("602645582fa6cb13f8449057")},{$push:{"threads.$.messages":"hello"}})
    

    如果它不起作用,请告诉我。请记住:它只会更新第一个匹配元素。

    【讨论】:

    • 我仍然收到WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 }),这真的让我很困惑,我想知道这是否是我的服务器的问题。我想不通,我想最明智的选择是将线程或消息放入他们自己的集合中。
    • @Lightningz1 我已经更新了我的答案。请再次检查
    • 还是不行。 mongoDB 文档说“从 MongoDB 4.4 开始,$ 投影运算符只能出现在字段路径的末尾;例如“field.$”或“fieldA.fieldB.$”。所以我没有尝试这条路线。 (docs.mongodb.com/manual/reference/operator/projection/…) 不过还是非常感谢你的帮助
    【解决方案2】:

    我真的很愚蠢,是拼写错误导致了我的问题。 group 不是grop,不是代码。

    捂脸并快速关闭线程

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2016-06-09
      • 1970-01-01
      • 2016-10-08
      相关资源
      最近更新 更多