Javascript MongoDB 选择_id 数组中的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7713363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:18:01  来源:igfitidea点击:

MongoDB select where in array of _id?

javascriptarraysnode.jsmongodbexpress

提问by itsme

is possible in mongo db to select collection's documents like in SQL :

可以在 mongo db 中像在 SQL 中一样选择集合的文档:

SELECT * FROM collection WHERE _id IN (1,2,3,4);

or if i have a _id arrayi must select one by one and then recompose the array/objectof results?

或者如果我有一个_id array我必须一一选择然后重新组合array/object结果?

回答by programmersbook

回答by KARTHIKEYAN.A

list is a array of ids

list 是一个 id 数组

In this code list is the array of ids in user collection

在此代码列表中是用户集合中的 id 数组

var list = ["5883d387971bb840b7399130","5883d389971bb840b7399131","5883d38a971bb840b7399132"]

    .find({ _id: {$in : list}})

回答by sourabhxiii

This is not related to mongo query. I was familiar with SQL and used Studio3T IDE to query a mongo database using SQL. If you are one like me, I want to point out that the id value needs to be typecasted. So the query will look like:

这与 mongo 查询无关。我熟悉 SQL 并使用 Studio3T IDE 使用 SQL 查询 mongo 数据库。如果你和我一样,我想指出 id 值需要类型转换。所以查询将如下所示:

SELECT _id from <collectionName>
WHERE _id = ObjectId("5883d387971bb840b7399130");