如何在 MongoDB 中获取 _id 数组?
MongoDB 中的 _id 是一个必备字段。在 MongoDB 中,每个存储在集合中的文档都需要一个唯一的 _id 字段,作为主键。以下是获取 MongoDB 中所有 id 数组的语法,即 _id
db.yourCollectionName.find({ _id : { $in : [yourValue1,yourValue2,yourValue3,.......N] } } );首先实现以下查询,以创建包含文档的集合
> db.selectInWhereIdDemo.insertOne({"_id":23});
{ "acknowledged" : true, "insertedId" : 23 }
> db.selectInWhereIdDemo.insertOne({"_id":28});
{ "acknowledged" : true, "insertedId" : 28 }
> db.selectInWhereIdDemo.insertOne({"_id":45});
{ "acknowledged" : true, "insertedId" : 45 }
> db.selectInWhereIdDemo.insertOne({"_id":75});
{ "acknowledged" : true, "insertedId" : 75 }
> db.selectInWhereIdDemo.insertOne({"_id":85});
{ "acknowledged" : true, "insertedId" : 85 }
> db.selectInWhereIdDemo.insertOne({"_id":145});
{ "acknowledged" : true, "insertedId" : 145 }以下是查询,以在集合中使用 find() 方法显示所有文档
> db.selectInWhereIdDemo.find().pretty();
这将产生以下输出
{ "_id" : 23 }
{ "_id" : 28 }
{ "_id" : 45 }
{ "_id" : 75 }
{ "_id" : 85 }
{ "_id" : 145 }以下是获取 _id 的查询,即数组中的所有 id
> db.selectInWhereIdDemo.find({ _id : { $in : [23,45,85,145] } } );这将产生以下输出
{ "_id" : 23 }
{ "_id" : 45 }
{ "_id" : 85 }
{ "_id" : 145 }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP