如何在 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" } ] } ] }
广告