Search code examples
node.jsmongodbmongoosemeanjs

How to find a record based on id in an array in mongoose


I am trying to fetch record based on the id present in an array but unfortunately the query fails.I have a record as follows My Schema,

 var EmployeehierarchySchema = new Schema({

  created_by: {
    type: Schema.ObjectId,
    ref: 'Employee'
  },
  parents: {
    type: Array,
    default: ''
  },
  childrens: {
    type: Array,
    default: ''
  },
});
 "parents" : [ 
    ObjectId("586e3bc35f01e338d7341304")
]

and i want fetch this record based on id and i wrote the following code

 var item = {'parents':req.id};
      Employeehierarchy.find(item).exec(function (err, employeehierarchy) {});

But i am getting empty eve i have the records.Can anyone suggest help.Yhanks.


Solution

  • Try this:

     Employeehierarchy.find({'parents':{$in:[req.id]}).exec(function (err, employeehierarchy) {});