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 }
] }热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬