我有两个集合,我在MongoDB中有一个集合数据
db.user.insert({"_id" : ObjectId("585d627fe4ff58cb1c456780"), "name":"Alfred"});
db.user.insert({"_id" : ObjectId("585d60b5e4ff58cb1c45677f"), "name":"Bruce"});
db.role.insert({"_id" : ObjectId("585d5f7cdb83cb696b82f2cf"),"status" : "admin"});
db.data.insert(
"security": [
{"idUser" : ObjectId("585d627fe4ff58cb1c456780"), "idRole" : ObjectId("585d5f7cdb83cb696b82f2cf")},
{"idUser" : ObjectId("585d60b5e4ff58cb1c45677f"), "idRole" : ObjectId("585d5f7cdb83cb696b82f2cf") }]);
当我执行此条件时,数组返回空
db.data.aggregate([
{ $lookup: {from: "user", localField: "user.idUser", foreignField: "_id", as: "user_admin"} },
{$unwind:"$user_admin"},
]);
我想把两个填充的字段带到数组中每个对象的属性,我非常感谢任何帮助我的人
答案 0 :(得分:0)
为了聚合安全性,您需要将其展开然后查找。此查询应该有效:
db.data.aggregate([
{$unwind: "$security"},
{$lookup: {from: "user", localField: "security.idUser", foreignField: "_id", as: "user_admin"}},
{$unwind:"$user_admin"}
]);