在 MongoDB 中获取特定多文档
要在 MongoDB 中获取特定多文档,请使用 $in。我们使用文档创建集合 −
> db.demo593.insertOne({id:1,"Name":"Chris"});{
"acknowledged" : true, "insertedId" : ObjectId("5e93177dfd2d90c177b5bcd9")
}
> db.demo593.insertOne({id:2,"Name":"John"});{
"acknowledged" : true, "insertedId" : ObjectId("5e931785fd2d90c177b5bcda")
}
> db.demo593.insertOne({id:3,"Name":"Bob"});{
"acknowledged" : true, "insertedId" : ObjectId("5e93178cfd2d90c177b5bcdb")
}
> db.demo593.insertOne({id:4,"Name":"Sam"});{
"acknowledged" : true, "insertedId" : ObjectId("5e931792fd2d90c177b5bcdc")
}借助 find() 方法从集合中显示所有文档 −
> db.demo593.find();
这将生成以下输出 −
{ "_id" : ObjectId("5e93177dfd2d90c177b5bcd9"), "id" : 1, "Name" : "Chris" }
{ "_id" : ObjectId("5e931785fd2d90c177b5bcda"), "id" : 2, "Name" : "John" }
{ "_id" : ObjectId("5e93178cfd2d90c177b5bcdb"), "id" : 3, "Name" : "Bob" }
{ "_id" : ObjectId("5e931792fd2d90c177b5bcdc"), "id" : 4, "Name" : "Sam" }以下是获取特定多文档的查询 −
> db.demo593.find({id:{$in:[1,3]}});这将生成以下输出 −
{ "_id" : ObjectId("5e93177dfd2d90c177b5bcd9"), "id" : 1, "Name" : "Chris" }
{ "_id" : ObjectId("5e93178cfd2d90c177b5bcdb"), "id" : 3, "Name" : "Bob" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP