【发布时间】:2020-06-21 11:12:31
【问题描述】:
我有这样的人, 这个人有很多公司,[ ONE TO MANY ]
{
"_id" : ObjectId("5eef12533167638883fba5ad"),
"companies" : [
ObjectId("00000000000000000011111") ,
ObjectId("0000000000000000022222")
],
"email" : "test@mailinator.com",
"phoneNumber" : "+1689999999999",
"createdAt" : ISODate("2020-06-21T07:54:56.529Z"),
"updatedAt" : ISODate("2020-06-21T07:54:56.529Z")
}
我想将公司汇总到公司数据中 我正在尝试这样
db.people.aggregate(
{ "$lookup": {
"from": "companies",
"localField": "companies",
"foreignField": "_id",
"as": "companies"
}},
)
但结果与查询 db.people.find() 相同 如何更正查询她在该数组中显示公司数据的方式,
我的预期是:
{
"_id" : ObjectId("5eef12533167638883fba5ad"),
"companies" : [
{
"_id": ObjectId("00000000000000000011111"),
"name": "Company one"
},
..... so on
],
"email" : "test@mailinator.com",
"phoneNumber" : "+1689999999999",
"createdAt" : ISODate("2020-06-21T07:54:56.529Z"),
"updatedAt" : ISODate("2020-06-21T07:54:56.529Z")
}
【问题讨论】:
标签: mongodb mongodb-query aggregate