对 MongoDB $in 查询的响应顺序? [英] Order of responses to MongoDB $in query?

查看:40
本文介绍了对 MongoDB $in 查询的响应顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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}});

返回结果的顺序是什么?有没有办法让我告诉 MongoDB我希望对结果进行排序,以便它们与 id_array 中的 id 的顺序相同?"

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?"

推荐答案

JIRA:

很快得到了很好的回应:使用 $or 而不是 $in

Quickly got a pretty good response: use $or instead of $in

c.find( { _id:{ $in:[ 1, 2, 0 ] } } ).toArray()

对比

c.find( { $or:[ { _id:1 }, { _id:2 }, { _id:0 } ] } ).toArray()

阅读错误报告了解更多信息.

Read the bug report for more info.

更新:

$or work-around hack 从 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 $in 查询的响应顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆