查询中对MongoDB $的响应顺序? [英] Order of responses to MongoDB $in query?
本文介绍了查询中对MongoDB $的响应顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ in 条件运算符不对顺序说任何话.如果我运行以下形式的查询
The MongoDB docs on the $in conditional operator don't say anything about order. If I run a query of the form
db.things.find({'_id': {'$in': id_array}});
返回结果的顺序是什么?
what will be the order of the returned results? And is there a way for me to tell MongoDB "I want the results sorted so that they're in the same order as the ids in id_array
?"
推荐答案
Quickly got a pretty good response: use $or
instead of $in
c.find( { _id:{ $in:[ 1, 2, 0 ] } } ).toArray()
vs.
c.find( { $or:[ { _id:1 }, { _id:2 }, { _id:0 } ] } ).toArray()
阅读错误报告以获取更多信息.
Read the bug report for more info.
更新:
$ or解决方法从2.6.x开始不再起作用-这是实现的副作用,它具有
The $or work-around hack no longer works starting with 2.6.x - it was a side effect of implementation which has changed.
这篇关于查询中对MongoDB $的响应顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文