是否可以通过查询中的以下条件进行查询?

时间:2016-02-17 01:40:36

标签: mongodb

我有一个用例是从MongoDB选择航线

但是我需要将搜索任务拆分为N个查询

FlightRoute.find({{"_id":{"airline":"Scoot","from":"KHH","to":"KIX"}})等等。

但是,使用REGEX语法进行以下查询也是不可能的。因为它涉及其他无关的数据

FlightRoute.find({{"_id":{"airline":/Scoot, Tiger, Peach/,"from":"KHH","to":/KIX, NRT/}})

这样做有什么好的解决方案吗?

[{"_id":{"airline":"Scoot","from":"KHH","to":"KIX"}},
 {"_id":{"airline":"Tiger","from":"KHH","to":"NRT"}},
 ...
 {"_id":{"airline":"Peach","from":"KHH","to":"KIX"}}]

1 个答案:

答案 0 :(得分:0)

使用$in

尝试
FlightRoute.find({
     '_id.airline': {$in: ['Scoot', 'Tiger', 'Peach']},
     '_id.from':'KHH',
     '_id.to': {$in: ['KIX', 'NRT']} });

结果

{ "_id" : { "airline" : "Scoot", "from" : "KHH", "to" : "KIX" } }
{ "_id" : { "airline" : "Tiger", "from" : "KHH", "to" : "NRT" } }
{ "_id" : { "airline" : "Peach", "from" : "KHH", "to" : "KIX" } }