如何在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 生日广州男生祝福语简短