如何在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"
}
}
}
以上是 如何在MongoDB文档中获取嵌入式数据? 的全部内容, 来源链接: utcz.com/z/350199.html