Home  >  Q&A  >  body text

How to traverse query in mongodb?

There is an existing array as follows,

const idArr = ['5950b7bfe78afb0868dc6599','5950b532b3c06f0838ed47e2','5950afcc85881f07bf454232'];
//数组每一项都作为查询的_id,遍历查询,该如何操作呢?

idArr.forEach((item,index)=>{
    db.details.find({_id:item}); //这样吗?还是。。。
});
天蓬老师天蓬老师2696 days ago1237

reply all(1)I'll reply

  • 黄舟

    黄舟2017-06-27 09:20:17

    The answer was found in the official documentation of mongodb

    const idArr = ['5950b7bfe78afb0868dc6599','5950b532b3c06f0838ed47e2','5950afcc85881f07bf454232'];
    db.details.find({_id:{$in:idArr}); //这样就可以了,一次全部查出来
    

    reply
    0
  • Cancelreply