我在猫鼬中定义了以下架构。
const location4Schema = new mongoose.Schema({
title: {
type: String,
unique: true
}
});
const location3Schema = new mongoose.Schema({
title: {
type: String,
unique: true
},
locations4: [location4Schema]
});
const location2Schema = new mongoose.Schema({
title: {
type: String,
unique: true
},
locations3: [location3Schema]
});
const location1Schema = new mongoose.Schema({
title: {
type: String,
unique: true
},
locations2: [location2Schema]
});
现在,我想使用特定位置3的 _id 查找位置4的数组。 我想要的结果是
[
{"_id": ObjectId("213213"), "title":"location4_title"},
{"_id": ObjectId("213213"), "title":"location4_title"},
...
]
目前我使用这种方式(同时使用location2 id和location3 id):
db.location1Schema.aggregate([
{"$unwind" : "$locations2"},
{$match: {"locations2._id": ObjectId("2131231")}},
{"$unwind" : "$locations2.locations3"},
{$match: {"locations2.locations3._id": ObjectId("21312321")}},
{$project:
{
"locations2.locations3.locations4._id":1,
"locations2.locations3.locations4.title":1, _id:0
}
}
])
**The result is:**
"locations2" : {
"locations3" : {
"locations4" : [
{
"_id" : ObjectId("213213"),
"title" : "some_title"
},
{
"_id" : ObjectId("2311"),
"title" : "some_title"
}
]
}
}
这是我的问题: