所以我的課程集合查詢屬性,是一個深度嵌套陣列
{
"_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")
]
},...
在本文檔以及我想要做的就是創建一個返回,在userId
有一個特定的userId
所有課程查詢數組,該數組位於steps
陣列中,用於特定的userId
。我試過使用$elemMatch
像這樣
Course.find({
"steps": {
"$elemMatch": {
"userId": {
"$elemMatch": "53554b56e3a1e1dc17db903f"
}
}
}
},
但它似乎是返回一個空白文檔。
這樣可以工作,但是你可以將它簡化爲'Course.find({'steps.userId':'53554b56e3a1e1dc17db903f'})'因爲你'不要比較每個元素的多個字段。 – JohnnyHK