Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do I perform the SQL Join equivalent in MongoDB

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 766
    Comment on it

    MongoDB does not have joins but sometimes we want references to documents in other collections. For this we have to use population. Population is the way of matching the specified paths in the document with document(s) from other collection(s).

    Let's look at below examples. Populating One Field If we want to populate a single field in the query for an object, we specify the name of that field in a string to the populate function:

    Player
      .findOne({ _id: 'abc123' })
      .populate('leagues')
      .exec (err, player) -> #..
    

    .Populating Multiple Fields Suppose we want to populate a multiple fields in the query for an object,we use below method:

    League
      .find()
      .populate('created_by players')
      .exec (err, league) -> #..
    

    cheers :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: