A
https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/#db.collection.aggregate I hope you help what I did:db.people.aggregate([{
$lookup: {
from: 'wallets',
let: { walletsID: '$_id' },
pipeline: [
{
$match: {
$expr: {
$in: [ '$$walletsID', '$members.people_id' ],
}
}
},
{
$lookup: {
from: 'type_coins',
localField: 'type_coins_id',
foreignField: '_id',
as: 'type_coin'
}
},
{ $unwind: { 'path': '$entrances', preserveNullAndEmptyArrays': true } },
{
$lookup: {
from: 'type_sports',
localField: 'entrances.type_sport',
foreignField: '_id',
as: 'entrances.type_sport'
}
},
{ $unwind: { 'path': '$entrances.type_sport', 'preserveNullAndEmptyArrays': true } },
{
$group: {
_id: "$_id",
name:{$first: "$name"},
members:{$first: "$members"},
type_coin:{$first: "$type_coin"},
entrances:{$addToSet: "$entrances"}
}
},
{ $unwind: { 'path': '$type_coin', 'preserveNullAndEmptyArrays': true } },
],
as: 'wallets'
}}])
Result:{
"_id" : ObjectId("5d27b7ba5455a11eacdf164d"),
"name" : {
"first" : "Alexssander ",
"last" : "Leal"
},
"user" : {
"active" : true,
"admin" : false,
"email" : "alex.leal.pls@gmail.com",
"password" : "123456"
},
"wallets" : [
{
"_id" : ObjectId("5d280149b1e77444a013c86b"),
"name" : "Bolsa esportiva",
"members" : [
{
"creator" : true,
"people_id" : ObjectId("5d27b7ba5455a11eacdf164d")
}
],
"type_coin" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff48c"),
"name" : "Real",
"abbreviation" : "BRL"
},
"entrances" : [
{
"event" : "Santos x Flamengo",
"stake" : 115,
"type_sport" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff568"),
"name" : "Futebol"
}
},
{
"event" : "Barcelona x Real Madrid",
"stake" : 50,
"type_sport" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff568"),
"name" : "Futebol"
}
}
]
},
{
"_id" : ObjectId("5d27fbf95a298d4470416f33"),
"name" : "Fundo de investimentos",
"members" : [
{
"creator" : true,
"people_id" : ObjectId("5d27b7ba5455a11eacdf164d")
}
],
"type_coin" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff48c"),
"name" : "Real",
"abbreviation" : "BRL"
},
"entrances" : [
{
"event" : "Bayer x Liverpool",
"stake" : 75,
"type_sport" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff568"),
"name" : "Futebol"
}
},
{
"event" : "Arsenal x Manchester United",
"stake" : 75,
"type_sport" : {
"_id" : ObjectId("5d1ebbb44a2b012e343ff568"),
"name" : "Futebol"
}
}
]
}
]}