Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

473
Views
Want to get object of reference collection by _id in mongodb node.JS

I want to get all detail of particular record by reference id collection.

{
"_id" : ObjectId("586c8bf63ef8480af89e94ca"),
"createdDate" : ISODate("2017-01-04T05:45:26.945Z"),
"branch" : {
    "$ref" : "branchmst",
    "$id" : "5864ac80fa769f09a4881791",
    "$db" : "eviral"
    },
"__v" : 0
}

This is my collection record. I need to all detail from "branchmst" collection where "_id" is "5864ac80fa769f09a4881791".

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your collection is an example of using manual references i.e. including one document's _id field in another document DBref. Mongoose can then issue a second query to resolve the referenced fields as needed.

The second query would be to use the aggregate method which has the $lookup operator that will perform a left outer join to "branchmst" collection in the same database to filter in documents from the "joined" collection for processing:

MyModel.aggregate([
    { "$match": { "branch": "5864ac80fa769f09a4881791" } },
    {
        "$lookup": {
            "from": "branchmst",
            "localField": "branch",
            "foreignField": "_id",
            "as": "branchmst"
        }
    },
    { "$unwind": "$branchmst" }
])

You can also use the populate() function in Mongoose given you have explicitly defined the refs in your Model definition i.e.

var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;

// define the main schema
var mySchema = mongoose.Schema({
    createdDate: { type: Date, default: Date.now },
    branch: { type: ObjectId, ref: 'Branch' }  
})

// define the branch schema
var branchSchema = mongoose.Schema({
    name: String,
    address: String  
})

// compile the models
var MyModel = mongoose.model('MyModel', mySchema),
    Branch = mongoose.model('Branch', branchSchema);

// populate query
MyModel.find({ "branch": "5864ac80fa769f09a4881791" })
       .populate('branch')
       .exec(function (err, docs) {
           //console.log(docs[0].branch.name);
           console.log(docs);
       });
over 4 years ago · Santiago Trujillo Report

0

provided you saved your branch.$id as type Schema Object.

 yourRecord.find({}).populate(branch.$id).exec(function(err, data){
  console.log(data)
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!