1
新的MEAN.js/Mongo開發人員在這裏,我真的很感謝任何關於此問題的見解。如何通過嵌套在多個數組中的_id訪問對象
下面是我正在使用的數據的相關部分,從文檔的頂部。我試圖深入到暫停數組內的對象的_id字段,以便我可以將它匹配到給定值(以訪問特定實例)並對其他屬性執行更新。
{
"_id" : ObjectId("575c902078c8db620d50699e"),
"username" : "superadmin",
"poms" : [
{
"_id" : ObjectId("577a6c6c1d4ddcf805e6a4a0"),
"pauses" : [
{
"_id" : ObjectId("577a6c6e1d4ddcf805e6a4a1"),
"momentResumed" : null,
"momentPaused" : ISODate("2016-07-04T14:02:22.643Z")
},
{
"_id" : ObjectId("577a6cae1d4ddcf805e6a4a2"),
"momentResumed" : null,
"momentPaused" : ISODate("2016-07-04T14:03:26.364Z")
}
],
"momentCompleted" : null,
"momentStarted" : ISODate("2016-07-04T14:02:20.383Z")
},
{
"_id" : ObjectId("577a6d63bc35cf1006c05f64"),
"pauses" : [
{
"_id" : ObjectId("577a6d64bc35cf1006c05f65"),
"momentResumed" : null,
"momentPaused" : ISODate("2016-07-04T14:06:28.685Z")
},
{
"_id" : ObjectId("577a6d67bc35cf1006c05f66"),
"momentResumed" : null,
"momentPaused" : ISODate("2016-07-04T14:06:31.869Z")
},
{
"_id" : ObjectId("577a6d7ebc35cf1006c05f67"),
"momentResumed" : null,
"momentPaused" : ISODate("2016-07-04T14:06:54.594Z")
}
],
"momentCompleted" : ISODate("2016-07-04T14:07:00.455Z"),
"momentStarted" : ISODate("2016-07-04T14:06:27.145Z")
}
]
}
這裏是聚集查詢,我想出了:
{ "$match": { "_id": req.user._id } },
{ "$unwind": "$poms" },
{ "$project": {
"_id": 0,
"pom": "$poms"
} },
{ "$match": { "pom._id": req.params.pomId } },
{ "$unwind": "$pom.pauses" },
{ "$project": {
"pause": "$pom.pauses"
} }
不幸的是它給空方括號。我真的很難過,我嘗試過所有我能想到的事情。你將如何從裏面「暫停」返回一個具有特定_id的對象?
任何幫助或意見將不勝感激。
這解決它,謝謝! – Jonathan