目前,我有三个相互关联的“用户”,“用户位置”和“用户地址”集合。
以下是架构
UserModel
var userTable = new Schema({
name: String,
email : String
});
module.exports = mongoose.model('user', userTable );
UserLocationModel
var userLocationTable = new Schema({
location : String,
user_id :{
type : ObjectId,
ref : 'user',
}
});
module.exports = mongoose.model('userLocation',userLocationTable);
UserAddressModel
var userAddressTable = new Schema({
userLocationID : {
type : ObjectID,
ref : 'userLocation',
},
address : String
});
module.exports = mongoose.model('userAddress',userAddressTable);
我的代码
UserModel.aggregate([
{
"$lookup": {
"from": "userlocations",
"localField": "_id",
"foreignField": "user_id",
"as": "location"
}
},
{
"$unwind" : "$location"
},
{
"$lookup": {
"from": "useraddresses",
"localField": "location._id",
"foreignField": "userLocationID",
"as": "location.address"
}
},
]).exec((err, data) => {
if (err) throw err;
res.send(data);
})
当前输出
[
{
"_id": "5d8b1e6ca4d1ab5e54f1b8ee",
"name": "Shivam",
"email": "ee@gmail.com",
"__v": 0,
"location": {
"_id": "5d8b1e6ca4d1ab5e54f1b8ef",
"location": "Mumbai",
"user_id": "5d8b1e6ca4d1ab5e54f1b8ee",
"__v": 0,
"address": []
}
},
{
"_id": "5d8b3a740b6fee55ac39a61d",
"name": "ameet",
"email": "ddt@gmail.com",
"__v": 0,
"location": {
"_id": "5d8b3a740b6fee55ac39a61e",
"location": "sion",
"user_id": "5d8b3a740b6fee55ac39a61d",
"__v": 0,
"address": []
}
}
]
这里的问题是我无法从UserAddress模型中获取数据。我尝试了所有可能的方法,但无法从UserAddress集合中获取数据。有人可以帮我吗?