如何在 mongoose 的数组中根据 id 查找记录

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

我正在尝试根据数组中存在的 id 获取记录,但不幸的是查询 fails.I 有如下记录 我的架构,

 var EmployeehierarchySchema = new Schema({

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

我想根据 id 获取这条记录,我写了下面的代码

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

但我在有 records.Can 任何人建议 help.Yhanks 的前夕感到空虚。

试试这个:

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