2

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?

Asad Ullah Khalid
  • 140
  • 1
  • 3
  • 13
  • what do you mean by "problem of matching the specific objectId " you need to insert `individual` object id into `usermodel` then call `populate()` – Thamaraiselvam Feb 28 '19 at 11:06

2 Answers2

8

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.

  • 1
    It worked for me. Back then I was writing the wrong syntax for "match" key. Thanks a lot for your help! – Asad Ullah Khalid Mar 05 '19 at 20:02
  • 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. – falcon Feb 23 '22 at 18:41
0

You add condition in populate

if you wanted to populate fans array based on their age, and return, at most, any 5 of them

.populate('fans', null, { age: { $gte: 21 }}, { limit: 5 })
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
  • 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 – Asad Ullah Khalid Mar 01 '19 at 06:23
  • oh you need to populate only specific individual? – Thamaraiselvam Mar 01 '19 at 06:33
  • 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 Mar 05 '19 at 20:00