Mongo,通过 id 列表查找 [英] Mongo, find through list of ids

查看:55
本文介绍了Mongo,通过 id 列表查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回字符串 MongoDB id 列表的进程,

I have a process that returns a list of String MongoDB ids,

[512d5793abb900bf3e20d012, 512d5793abb900bf3e20d011]

我想向 Mongo 发出一个查询,并按照与列表相同的顺序取回匹配的文档.

And I want to fire a single query to Mongo and get the matching documents back in the same order as the list.

执行此操作的 shell 符号是什么?

What is the shell notation to do this?

推荐答案

将字符串转换为 ObjectIds 后,可以使用 $in 运算符以获取列表中的文档.没有任何查询符号可以按照列表的顺序返回文档,但请参阅 在这里了解一些处理方法.

After converting the strings into ObjectIds, you can use the $in operator to get the docs in the list. There isn't any query notation to get the docs back in the order of your list, but see here for some ways to handle that.

var ids = ['512d5793abb900bf3e20d012', '512d5793abb900bf3e20d011'];
var obj_ids = ids.map(function(id) { return ObjectId(id); });
db.test.find({_id: {$in: obj_ids}});

这篇关于Mongo,通过 id 列表查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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