【发布时间】: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