Blog

  • Uses of Lookup In MongoDb

    Posted by Pradeep Singh | Last Updated: 01-Jun-18

    Introduction:-

    In MongoDB, 3.2v has delivered $lookup operator in aggregation framework which may be utilized to carry out LEFT join. Please notice that MongoDB data operation isn't always like working facts in a relational database so it's far always advisable to have applicable data to be within the same report those often the usage of together. however, in a certain case, it's far required to put in two specific collections due to numerous like, to keep away from facts duplication for which we've heavy update operations, use in a couple of collections and many others. Join is a diffrence between NoSQL and SQL database wherein as MongoDB $lookup Aggregation operator is effective and useful to get be part of for two collections like doing it in RDBMS.

     

    Syntax:-

    db.(collection_Name).aggregation([
                    {
                            $lookup:{
                from: "(foreign_collection)",
                        localField : "(local_field)",
                        foreignField : "(foreign_collection_field_name)",
                        as : "(key_name_to_appear_in_result)"
            }
        }
    ])

     

    If you are from Relational DataBase Management System world then you will be expecting this as separate Document link all fields from the foreign and local collections but NO, all foreign documents will be introduced as an array of nearby collection file. because all foreign Document introduced in the report you would need to use best if a number of foreign relative files are constrained and now not be making the size of end result document to exceed document length limit.

    Left outer join

     

     

     

     

     

     

    Let say we have two collections college and students like this

    College collection:

    {
        "id":"01",
            "collegeName":"SRGOC",
            "address" : "Kota",
            "students":["012","124","156"]
    },
    {
        "id":"02",
            "collegeName":"IIITM",
            "address" : "Gwalior",
            "students":["012","123","198"]
    }

    Students collection:-

    {"_id":"012":"name":"rahul","rollNo":159},
    {"_id":"124":"name":"vikas","rollNo":160},
    {"_id":"123":"name":"mohit","rollNo":162},
    {"_id":"156":"name":"sarjib","rollNo":161},
    {"_id":"198":"name":"sachin","rollNo":165},
    {"_id":"201":"name":"pradeep","rollNo":168}

     

     

    Now retrive the college with students details:

    db.college.aggregate({
            $lookup:
    {
        from: "students",
                localField: "studentIds",
            foreignField : "_id",
            as: "students"
    }
    });

    So Result will be like this:-

    {
        "_id":"01",
            "collegeName":"SRGOC",
            "address" : "Kota",
            "students":[
        {"_id":"012":"name":"rahul","rollNo":159},
        {"_id":"124":"name":"vikas","rollNo":160},
        {"_id":"156":"name":"sarjib","rollNo":161}
                  ]
    },
    {
        "_id":"02",
            "collegeName":"IIITM",
            "address" : "Gwalior",
            "students":[
        {"_id":"012":"name":"rahul","rollNo":159},
        {"_id":"123":"name":"mohit","rollNo":162},
        {"_id":"198":"name":"sachin","rollNo":165}
                 ]
    }

Tags: nosql