简体   繁体   English

Node JS mongodb,猫鼬找到多个项目

[英]Node js mongodb, mongoose find multiple items

So I wanna find multiple items so I don't have to loop sense I was told not to do that. 因此,我想查找多个项目,因此不必循环告诉我不要这样做。

{ "_id" : ObjectId("59d2b1cf8cec1709f85eb7a9"), "title" : "Arrow", "common_movies" : [ ], "__v" : 0 }
{ "_id" : ObjectId("59d2b1cf8cec1709f85eb7aa"), "title" : "Gotham", "common_movies" : [ ], "__v" : 0 }

my database's movie collection looks like this. 我数据库的电影收藏看起来像这样。 and I want an array with 2 items in it, the Arrow and Gotham objects, This is what I tried 我想要一个包含2个项目的数组,即Arrow和Gotham对象,这就是我尝试的

Movie.find({title: "Arrow"}, {title: "Gotham"}, function(err, foundMovie){
    console.log(foundMovie)
});

If you want to select movies where name is Arrow or Gotham, then you should use or operator in your criteria object: var criteria = { $or: [ {title: 'Arrow'}, {title: 'Gotham'} ]} . 如果要选择名称为Arrow或Gotham的电影,则应在条件对象中使用or运算符: var criteria = { $or: [ {title: 'Arrow'}, {title: 'Gotham'} ]} See mongodb documentation about OR logical operator . 请参阅有关OR逻辑运算符的mongodb文档

Then use criteria object in your find method: 然后在您的find方法中使用criteria对象:

Movie.find(criteria, function(err, foundMovie){
    console.log(foundMovie)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM