I have Transaction
collection like this:
{
_id: ObjectId("5f1284078a7dd8a6b9140c97")
company: ObjectId("5f127ce1e354f37df698f55e")
person: ObjectId("5f1284078a7dd8a6b9140c95")
actionTransaction: "Purchase"
}
I have Companies
like this :
{
_id: ObjectId("5f127ce1e354f37df698f55e")
name: "MongoDB Inc"
phoneNumber: "+1111111111"
members: [
{
_id: ObjectId("5f1284078a7dd8a6b9140c95")
title: "CEO"
role: "ADMIN"
},
{
_id: ObjectId("5f1284078a7ff8a6b9300d93")
title: "CHAIRMAN"
role: "Others"
}
]
}
and I have ‘People’ collection like this:
{
_id: ObjectId("5f1284078a7dd8a6b9140c95")
name: "Foo Bar"
gender: "Male"
}
when I do query like this:
db.Transaction.aggregate(
[
{ $lookup:
{
from: "companies",
localField: "company",
foreignField: "_id",
as: "company"
}
},
{ $lookup:
{
from: "people",
localField: "person",
foreignField: "_id",
as: "person"
}
},
]
)
I got result an Array
like this :
[
_id: ObjectId("5f1284078a7dd8a6b9140c97")
company: [
{
_id: ObjectId("5f127ce1e354f37df698f55e")
name: "MongoDB Inc"
phoneNumber: "+1111111111"
members: [
{
_id: ObjectId("5f1284078a7dd8a6b9140c95")
title: "CEO"
role: "ADMIN"
},
{
_id: ObjectId("5f1284078a7ff8a6b9300d93")
title: "CHAIRMAN"
role: "Others"
}
]
}
],
person: [
{
_id: ObjectId("5f1284078a7dd8a6b9140c95")
name: "Foo Bar"
gender: "Male"
}
]
]
you can see each relations there the result on that fields are Array
,
and I really one also on that Person
field on Transaction Collection
gonna get the role
and title
which from Company
but I have no idea to filter it into that Person field
this is the illustration the result I want just an Object
:
// Transaction
{
company : { COMPANY_DATA },
transactions: [
member: {
_id: ObjectId("5f1284078a7dd8a6b9140c95")
name: "Foo Bar"
gender: "Male"
title: "CEO"
role: "ADMIN"
},
actionTransaction: "Purchase"
]
}
is it possible doing this ? any idea for doing it to possibble??