Mongodb Aggregate

Asked

Viewed 25 times

0

have that collection: Students

{
    "_id" : ObjectId("602fc88812985223d0f7ac5f"),
    "guardians" : [ 
        {
            "_id" : ObjectId("602fc88812985223d0f7ac60")
        }, 
        {
            "_id" : ObjectId("602fcb9a063fcb30d4017f8a")
        }
    ]
},
{
    "_id" : ObjectId("602fcb9a063fcb30d4017f89"),
    "guardians" : [ 
        {
            "_id" : ObjectId("602fcb9a063fcb30d4017f8b")
        }
    ]
}

that second collection entities has the references for the registration of students and responsible (field Guardians in the collection Students)

{
    "_id" : ObjectId("602fc88812985223d0f7ac5f"),
    "name" : "Estudante Maria"
},
{
    "_id" : ObjectId("602fc88812985223d0f7ac60"),
    "name" : "Responsavel Paulo Maria"
},
{
    "_id" : ObjectId("602fcb9a063fcb30d4017f8a"),
    "name" : "Responsavel Ana Maria"
},
{
    "_id" : ObjectId("602fcb9a063fcb30d4017f89"),
    "name" : "Estudante Joao"
},
{
    "_id" : ObjectId("602fcb9a063fcb30d4017f8b"),
    "name" : "Responsavel Maria Joao"
}

precise result

{
    "_id" : ObjectId("602fc88812985223d0f7ac5f"),
    "entity": {
        "_id" : ObjectId("602fc88812985223d0f7ac5f"),
        "name" : "Estudante Maria"
    }
    "guardians" : [ 
        {
            "_id" : ObjectId("602fc88812985223d0f7ac60"),
            "entity": {
                "_id" : ObjectId("602fc88812985223d0f7ac60"),
                "name" : "Responsavel Paulo Maria"
            }
        }, 
        {
            "_id" : ObjectId("602fcb9a063fcb30d4017f8a"),
            "entity": {
                "_id" : ObjectId("602fcb9a063fcb30d4017f8a"),
                "name" : "Responsavel Ana Maria"
            }
        }
    ]
},
{
    "_id" : ObjectId("602fcb9a063fcb30d4017f89"),
    "entity": {
        "_id" : ObjectId("602fcb9a063fcb30d4017f89"),
        "name" : "Estudante Joao"
    }
    "guardians" : [ 
        {
            "_id" : ObjectId("602fcb9a063fcb30d4017f8b"),
            "entity": {
                "_id" : ObjectId("602fcb9a063fcb30d4017f8b"),
                "name" : "Responsavel Maria Joao"
            }
        }
    ]
}

the student-related Entity I get with this instruction

db.students.aggregate([
    {
        $lookup: {
            from: 'entities',
            localField: '_id',
            foreignField: '_id',
            as: 'entity'
            }
        },
    {
        $unwind: '$entity'
        }
])

but the entities concerning the responsible cannot

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.