lookup operation in mongo

Write a Query to lookup operation on students collection with sports collection.

Sample Document For students collection:

{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc2"), 
    "id" : 1.0, 
    "pupil" : "John", 
    "std" : 6.0, 
    "ht" : 153.0, 
    "wt" : 43.0
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc3"), 
    "id" : 2.0, 
    "pupil" : "Jack", 
    "std" : 6.0, 
    "ht" : 164.0, 
    "wt" : 54.0
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc4"), 
    "id" : 3.0, 
    "pupil" : "Jill", 
    "std" : 6.0, 
    "ht" : 173.0, 
    "wt" : 69.0
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc5"), 
    "id" : 4.0, 
    "pupil" : "william", 
    "std" : 6.0
}


Sample Document for sports collection:

{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d008e"), 
    "id" : 1.0, 
    "sport" : "Basketball", 
    "winner" : "John"
}
{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d008f"), 
    "id" : 2.0, 
    "sport" : "TT", 
    "winner" : "Jack"
}
{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d0090"), 
    "id" : 3.0, 
    "sport" : "Tennis", 
    "winner" : "John"
}
{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d0091"), 
    "id" : 4.0, 
    "sport" : "carrom", 
    "winner" : "william"
}
{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d0092"), 
    "id" : 5.0, 
    "sport" : "fencing", 
    "winner" : "william"
}
{ 
    "_id" : ObjectId("602f2d5aa1d36ac7807d0093"), 
    "id" : 6.0
}

Approach

Query: Query to perform lookup operation on two collections.


db.students.aggregate([
    {
    $lookup:
    {
    from : "sports",
    localField : "pupil",
    foreignField : "winner",
    as : "games"
    } } ] )

Output: 

{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc2"), 
    "id" : 1.0, 
    "pupil" : "John", 
    "std" : 6.0, 
    "ht" : 153.0, 
    "wt" : 43.0, 
    "games" : [
        {
            "_id" : ObjectId("602f2d5aa1d36ac7807d008e"), 
            "id" : 1.0, 
            "sport" : "Basketball", 
            "winner" : "John"
        }, 
        {
            "_id" : ObjectId("602f2d5aa1d36ac7807d0090"), 
            "id" : 3.0, 
            "sport" : "Tennis", 
            "winner" : "John"
        }
    ]
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc3"), 
    "id" : 2.0, 
    "pupil" : "Jack", 
    "std" : 6.0, 
    "ht" : 164.0, 
    "wt" : 54.0, 
    "games" : [
        {
            "_id" : ObjectId("602f2d5aa1d36ac7807d008f"), 
            "id" : 2.0, 
            "sport" : "TT", 
            "winner" : "Jack"
        }
    ]
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc4"), 
    "id" : 3.0, 
    "pupil" : "Jill", 
    "std" : 6.0, 
    "ht" : 173.0, 
    "wt" : 69.0, 
    "games" : [

    ]
}
{ 
    "_id" : ObjectId("602f2db6376b261d1d6addc5"), 
    "id" : 4.0, 
    "pupil" : "william", 
    "std" : 6.0, 
    "games" : [
        {
            "_id" : ObjectId("602f2d5aa1d36ac7807d0091"), 
            "id" : 4.0, 
            "sport" : "carrom", 
            "winner" : "william"
        }, 
        {
            "_id" : ObjectId("602f2d5aa1d36ac7807d0092"), 
            "id" : 5.0, 
            "sport" : "fencing", 
            "winner" : "william"
        }
    ]
}

No comments:

Post a Comment