如何在MongoDB中创建双嵌套数组?
要在MongoDB中创建双嵌套数组,让我们实现查询以创建包含文档的集合。在其中,我们创建了一个显示学生详细信息的双嵌套数组,其中包含项目名称和用于开发同一项目的技术:
> db.doubleNestedArrayDemo.insertOne( ... { ... "StudentId" : "1000", ... "StudentName" : "Larry", ... "StudentDetails" : [ ... { ... "ProjectName" : "Online Banking", ... "ProjectDetails" : [ ... { ... "TechnologyUsed" : "Java" ... }, ... { ... "TechnologyUsed" : "MySQL in Backend" ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c992bd7330fd0aa0d2fe4cc") } > db.doubleNestedArrayDemo.insertOne( { "StudentId" : "1001", "StudentName" : "Robert", "StudentDetails" : [ { "ProjectName" : "Student Web Tracker", "ProjectDetails" : [ { "TechnologyUsed" : "Django Framework" }, { "TechnologyUsed" : "MongoDB in Backend" } ] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5c992cdb330fd0aa0d2fe4cd") }
以下是在find()
方法的帮助下显示集合中所有文档的查询
> db.doubleNestedArrayDemo.find().pretty()
这将产生以下输出
{ "_id" : ObjectId("5c992bd7330fd0aa0d2fe4cc"), "StudentId" : "1000", "StudentName" : "Larry", "StudentDetails" : [ { "ProjectName" : "Online Banking", "ProjectDetails" : [ { "TechnologyUsed" : "Java" }, { "TechnologyUsed" : "MySQL in Backend" } ] } ] } { "_id" : ObjectId("5c992cdb330fd0aa0d2fe4cd"), "StudentId" : "1001", "StudentName" : "Robert", "StudentDetails" : [ { "ProjectName" : "Student Web Tracker", "ProjectDetails" : [ { "TechnologyUsed" : "Django Framework" }, { "TechnologyUsed" : "MongoDB in Backend" } ] } ] }