0

Here's my Schema

var PositiveSchema = new mongoose.Schema({
    schoolID: {
        type: mongoose.Schema.Types.ObjectId, ref: 'School'
    },
    name: String,
    restrictAwardTo: Object
})

Now restrictAwardTo saves the data in this format

"restrictAwardTo" : [ 
        {
            "_id" : "5c31907d908eb8404657cbf0",
            "firstName" : "Admin 2a"
        }, 
        {
            "_id" : "5c1a7677c98da061141475a8",
            "firstName" : "Justice"
        }, 
        {
            "_id" : "5c1a7677c98da061141475a9",
            "firstName" : "Itik"
        }
    ],

How can I search inside my document using one of the _id listed under restrictAwardTo? I tried the solutions given below

Update: In Robo3t, this query db.getCollection('positives').find({ 'restrictAwardTo._id' : {$in: ['5c1a7677c98da061141475a7']} }) works. Now I'm making it work for mongoose too.

imin
  • 4,504
  • 13
  • 56
  • 103

2 Answers2

0

Here's the mongoose that works for me:

Positive.find({ schoolID:  mongoose.mongo.ObjectId(schoolID), "restrictAwardTo._id": { $in: [userID]} })

But I'm not entirely sure of the performance for large records.

imin
  • 4,504
  • 13
  • 56
  • 103
0

You could go through this way.

Positive.findOne({'restrictAwardTo': {$elemMatch: {_id: userID}}}, 
        (err,schoolInfo) => { });