MMR
MMR

Reputation: 3029

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.

Upvotes: 0

Views: 575

Answers (1)

Simran
Simran

Reputation: 2830

Try this:

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

Upvotes: 1

Related Questions