mongodbmongodb-atlasmongoose-populate

How can I populate referenced Objects in a MongoDB Atlas query?


I have a collection in MongoDB Atlas that I've created using an app I'm building. The collection is called "timeRecords", and each document has an an "experience" field, which is an ObjectID that references an experience document in the "experience" collections.

In node.js, I populate this by executing a search query then chain .populate() to the end of the query. My question is whether there's a way to do this via the UI on MongoDB Atlas' website?

enter image description here

I've tried using the "More Options" but I can't figure this out. I want to go into the database and search for documents without needing to know the exact object ID. For example, each experience is categorized by "easy", "intermediate" and "expert", I want to search by all "experience.expert" documents, except the "experience" field isn't populated.


Solution

  • populate is mongooses specific thing so you won't be able to use it in mongo shell

    quoting the docs

    Populate

    MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

    for your case you can use the aggregation tab in atlas.

    for example like

    db.timeRecords.aggregate([
      {
        $lookup: {
          from: "experience",  //collection to populate
          localField: "experience", //local field you want to match
          foreignField: "_id", //field to match of the collection you are going to populate
          as: "experienceData" // populated field stored in this field
        }
      }
    ])
    

    demo