给定一个像这样的集合:
> db.test.find({}, {_id:0})
{ "name" : "star wars I", "duration" : 120 }
{ "name" : "star wars II", "duration" : 125 }
{ "name" : "star wars III", "duration" : 135 }
{ "name" : "star wars IV", "duration" : 115 }
{ "name" : "star trek I", "duration" : 135 }
{ "name" : "star trek II", "duration" : 200 }
{ "name" : "alien", "duration" : 200 }
如何进行与此 SQL 语句等效的查询?
SELECT * FROM test
WHERE (name LIKE '%star%' AND NOT name LIKE '%wars%') OR duration = 200
即使使用更简单的查询,我也无法获得预期的结果,例如:
这按预期工作:
> db.test.find({ name: /star/i, name: /trek/i }, {_id:0})
{ "name" : "star trek I", "duration" : 135 }
{ "name" : "star trek II", "duration" : 200 }
但是这个没有返回任何结果。为什么?
> db.test.find({ name: /star/i, $not: {name: /trek/i} }, {_id:0})