如何获取 MongoDB 文档中的嵌入数据?
mongodbdatabasebig data analytics
以下是获取 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"
}
}
}

