Match specific value in mongoose populate

16,915

You have a reference of 'individual' in supporterOf and you want to populate only relevant object from the array of individuals? If this is right case then do the following:

YourUserModel.findById(userId, function (err, user) {
        console.log(user)
     }).populate({
        path: 'supporterOf',
        match: {
           yourObjectOfIndividualDocument: yourMatchingIdOfIndividual
        }
     })
     .exec()

Replace yourObjectOfIndividualDocument: yourMatchingIdOfIndividual by name: 'abcd'. Here name is the field of Individual document not of User document.

Share:
16,915

Related videos on Youtube

Asad Ullah Khalid
Author by

Asad Ullah Khalid

I am a Software Development Engineer with a BS degree in Computer Science. I have ~3 years of professional experience developing Full Stack Web Applications on technologies like VueJs, ReactJs, NodeJs, ExpressJs, MySQL, MongoDB, etc. Having experience in multiple technologies requires a strong understanding of the software development life cycle, core knowledge of the languages, and best practices followed by the best in the world. So I always push myself to not lack any of it by staying active in the community and contributing to open source. I try to learn new stuff every day and implement it in my current work to get better and better at what I do.

Updated on June 04, 2022

Comments

  • Asad Ullah Khalid
    Asad Ullah Khalid almost 2 years

    Following is the schema of a user collection:

    const Mongoose = require('mongoose')
    
    const Schema = Mongoose.Schema
    
    const userSchema = new Schema({
        name: {
            type: String,
            required: true
        },
        email: {
            type: String,
            required: true
        },
        password: {
            type: String
        },
        supporterOf: [{
            type: Schema.Types.ObjectId,
            ref: 'individual',
            required: false
        }],
    })
    
    module.exports = Mongoose.model('user', userSchema);
    

    I want to populate 'supporterOf' which is a collection of individual (ref: individual). The 'supporterOf' contains an array of ObjectId. I am having the problem of matching the specific objectId with that ObjectId array. Can anyone suggest me how I can match specific ObjectId with an array of ObjectIds in populate function?

    • Thamaraiselvam
      Thamaraiselvam about 5 years
      what do you mean by "problem of matching the specific objectId " you need to insert individual object id into usermodel then call populate()
  • Asad Ullah Khalid
    Asad Ullah Khalid about 5 years
    No. When you are populating individual, consider individual as an array of ObjectId, it should populate only the index (lets say index 3) of that array. However, there is a populate object key called match. I am using it to compare the array index with user generated id to compare but I guess I am writing wrong syntax
  • Thamaraiselvam
    Thamaraiselvam about 5 years
    oh you need to populate only specific individual?
  • Asad Ullah Khalid
    Asad Ullah Khalid about 5 years
    Yes I wanted to add a condition in populate function which I could have achieved by "match" key but I wasn't able to write the right syntax for matching the two ObjectIds but the other answer on this thread worked for me. Though, I appreciate your help. Thanks!
  • Asad Ullah Khalid
    Asad Ullah Khalid about 5 years
    It worked for me. Back then I was writing the wrong syntax for "match" key. Thanks a lot for your help!
  • falcon
    falcon about 2 years
    Is this possible YourUserModel.findById(userId, function (err, user) { console.log(user) }).populate({ path: 'supporterOf', match: { 'supporterOf.someFields': 'something' } }) .sort(sortingObject) ? I want to use the populated field in match query. Is this possible? My problem is that I want to filter based on populated data and then sort it.