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
MongoDB select where in array of _id?
提问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 array
i must select one by one and then recompose the array/object
of results?
或者如果我有一个_id array
我必须一一选择然后重新组合array/object
结果?
回答by programmersbook
Easy :)
简单 :)
db.collection.find( { _id : { $in : [1,2,3,4] } } );
taken from: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
取自:http: //www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
回答by KARTHIKEYAN.A
回答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");