在 MongoDB 中检查现有文档或嵌入文档
要检查现有文档或嵌入文档,请在 MongoDB 中使用 $exists。让我们创建一个包含文档的集合 −
> db.demo322.insertOne(
... {'id':1001,
... 'details':[{'Score':10000,Name:"Bob"},
... {'Score':98000,Name:"Sam"}
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5113e2f8647eb59e56206c")
}
> db.demo322.insertOne(
... {'id':10002,
... 'details':[{'Score':9000},
... {'Score':91000}
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e5113faf8647eb59e56206d")
}在集合中使用 find() 方法显示所有文档 −
> db.demo322.find();
这将生成以下输出 −
{
"_id" : ObjectId("5e5113e2f8647eb59e56206c"), "id" : 1001, "details" : [
{ "Score" : 10000, "Name" : "Bob" }, { "Score" : 98000, "Name" : "Sam" }
]
}
{
"_id" : ObjectId("5e5113faf8647eb59e56206d"), "id" : 10002, "details" : [
{ "Score" : 9000 }, { "Score" : 91000 }
]
}以下是检查现有文档或嵌入文档的查询 −
> db.demo322.find({"details.Name":{$exists:true}}).count() > 0;这将生成以下输出 −
True
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP