如何在MongoDB文档中获取嵌入式数据?
以下是在MongoDB文档中获取嵌入数据的语法
db.yourCollectionName.find({},{‘yourOuterKeyName.yourInnerKeyName:1}).pretty();首先让我们创建一个包含文档的集合
> db.embeddedCollectionDemo.insertOne(
... {
... "StudentName" : "Larry",
... "StudentDetails": {
... "Larry1234": {"ProjectName": "Student Web Tracker"},
... "Larry7645": {"ProjectName": "Hospital Management System"},
... "Larry9879": {"ProjectName": "Library Management System"},
...
... }
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5c98a100330fd0aa0d2fe4c5")
}以下是查询以显示集合中的所有文档
> db.embeddedCollectionDemo.find().pretty();
这将产生以下输出
{
"_id" : ObjectId("5c98a100330fd0aa0d2fe4c5"),
"StudentName" : "Larry",
"StudentDetails" : {
"Larry1234" : {
"ProjectName" : "Student Web Tracker"
},
"Larry7645" : {
"ProjectName" : "Hospital Management System"
},
"Larry9879" : {
"ProjectName" : "Library Management System"
}
}
}以下是对嵌入式集合的查询,即MongoDB集合中的嵌入式数据
> db.embeddedCollectionDemo.find({},{'StudentDetails.Larry7645':1}).pretty();这将产生以下输出
{
"_id" : ObjectId("5c98a100330fd0aa0d2fe4c5"),
"StudentDetails" : {
"Larry7645" : {
"ProjectName" : "Hospital Management System"
}
}
}热门推荐
10 朋友儿子点灯祝福语简短
11 生日高考祝福语妹妹简短
12 车队生日条幅祝福语简短
13 女儿蛋糕祝福语简短精辟
14 生日贺卡祝福语英文简短
15 会计祝福语毕业英语简短
16 农村文化祝福语大全简短
17 成语祝福语精选大全简短
18 火把节祝福语简短