用于嵌套文档的 MongoDB find() 查询?
如要从嵌套文档中获取值,请使用点符号。让我们使用文档创建集合 −
> db.demo591.insert([
... { "Name": "John", "Age": 23 },
... {"Name": "Carol", "Age": 26},
... { "Name": "Robert", "Age": 29,
... details:[
... {
... Email:"Robert@gmail.com",CountryName:"US"},{"Post":35}
... ]}
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})使用 find() 方法显示集合中的所有文档 −
> db.demo591.find();
这将生成以下输出 −
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd3"), "Name" : "John", "Age" : 23 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd4"), "Name" : "Carol", "Age" : 26 }
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [ {
"Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 } ] }以下是使用点符号获取嵌套文档的查询 −
> db.demo591.find({"details.Email": "Robert@gmail.com"});这将生成以下输出 −
{ "_id" : ObjectId("5e92dd08fd2d90c177b5bcd5"), "Name" : "Robert", "Age" : 29, "details" : [
{ "Email" : "Robert@gmail.com", "CountryName" : "US" }, { "Post" : 35 }
] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP