gpt4 book ai didi

node.js - 查询深度嵌套数组中的属性

转载 作者:可可西里 更新时间:2023-11-01 09:14:21 24 4
gpt4 key购买 nike

所以我在类(class)集中有这个文档

{
"_id" : ObjectId("53580ff62e868947708073a9"),
"startDate" : ISODate("2014-04-23T19:08:32.401Z"),
"scoreId" : ObjectId("531f28fd495c533e5eaeb00b"),
"rewardId" : null,
"type" : "certificationCourse",
"description" : "This is a description",
"name" : "testingAutoSteps1",
"authorId" : ObjectId("532a121e518cf5402d5dc276"),
"steps" : [
{
"name" : "This is a step",
"description" : "This is a description",
"action" : "submitCategory",
"value" : "532368bc2ab8b9182716f339",
"statusId" : ObjectId("5357e26be86f746b68482c8a"),
"_id" : ObjectId("53580ff62e868947708073ac"),
"required" : true,
"quantity" : 1,
"userId" : [
ObjectId("53554b56e3a1e1dc17db903f")
]
},...

我想做的是创建一个查询,返回在 stepsuserId 数组中具有特定 userId 的所有类(class)> 特定 userId 的数组。我试过像这样使用 $elemMatch

Course.find({ 
"steps": {
"$elemMatch": {
"userId": {
"$elemMatch": "53554b56e3a1e1dc17db903f"
}
}
}
},

但它似乎返回一个空文档。

最佳答案

我认为这对你有用,你的语法有点偏离,而且你需要使用 ObjectId():

db.Course.find({ steps : { $elemMatch: { userId:ObjectId("53554b56e3a1e1dc17db903f")} } })

关于node.js - 查询深度嵌套数组中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23275722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com