0

Here is my schema

var mongoose = require('mongoose');

var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var schema = new mongoose.Schema({          
       month:String,
       year:String,
       endDate:Date,
       bio:[{
           _id:false,
           id:String,
           date:Date,
           inTime:Date,
           outTime:Date,
           workingHours:String
       }],
       createdBy:ObjectId   
},{timestamps:true});

schema.index({month: 1, year: 1 }, { unique: true });

module.exports=mongoose.model('bioMatrix',schema);

i want to get only bio data whose id is say "18" I am trying like

   BioMatrix.find({month:11,year:2016,'bio.id':"18"},{bio:1,_id:0})     
   .exec(function(e,b){           
       console.log(JSON.stringify(b[0]))
   })

But it show all the bio data wich is around 1000

How to find only filter array

also tried

BioMatrix.find( { bio : { $elemMatch: {  id : "18" } } })
 .exec(function(e,b){           
       console.log(e)
       console.log(JSON.stringify(b))
   })
  • As one of the answers in the marked duplicate suggests, you need to use the `$ positional operator` in your projection as `BioMatrix.find( { bio : { $elemMatch: { id : "18" } } }, {'bio.$': 1}).exec(callback);` – chridam Jan 12 '17 at 12:29
  • it is working , but only first object of bio is coming not all – Ashutosh Jha Jan 12 '17 at 12:41
  • Done BioMatrix.aggregate([ { $match: {month:"10"}}, { $project: { bio: {$filter: { input: '$bio', as: 'item', cond: { $eq: [ "$$item.bioId", 18 ] } }} }} ],function(err,usr){ console.log(err) console.log(JSON.stringify(usr)) }) – Ashutosh Jha Jan 12 '17 at 13:49

0 Answers0