使用文档从 MongoDB 集合中评估一个或多个值
若要评估一个或多个值,请使用 $or 与 find() 一起使用。让我们使用文档创建集合 −
> db.demo174.insertOne({"StudentName":"Chris","CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e383c709e4f06af551997e5")
}
> db.demo174.insertOne({"StudentName":"David","CountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e383c779e4f06af551997e6")
}
> db.demo174.insertOne({"StudentName":"Bob","CountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e383c7e9e4f06af551997e7")
}在集合中显示所有文档,在 find() 方法的帮助下 −
> db.demo174.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e383c709e4f06af551997e5"), "StudentName" : "Chris", "CountryName" : "US" }
{ "_id" : ObjectId("5e383c779e4f06af551997e6"), "StudentName" : "David", "CountryName" : "UK" }
{ "_id" : ObjectId("5e383c7e9e4f06af551997e7"), "StudentName" : "Bob", "CountryName" : "AUS" }以下是用于评估一个或多个值的查询。在此处,我们获取不止一个值,即学生“David”或国家“US” −
> db.demo174.find({$or:[{"StudentName":"David"},{"CountryName":"US"}]});这将产生以下输出 −
{ "_id" : ObjectId("5e383c709e4f06af551997e5"), "StudentName" : "Chris", "CountryName" : "US" }
{ "_id" : ObjectId("5e383c779e4f06af551997e6"), "StudentName" : "David", "CountryName" : "UK" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP