在 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" } ] } ] }
以下查询用于在双重嵌套数组中进行搜索。我们正在查找文本“Java”的记录
>db.doubleNestedArrayDemo.find({"StudentDetails.ProjectDetails.TechnologyUsed":"Java"}).pretty();
这会产生以下输出
{ "_id" : ObjectId("5c992bd7330fd0aa0d2fe4cc"), "StudentId" : "1000", "StudentName" : "Larry", "StudentDetails" : [ { "ProjectName" : "Online Banking", "ProjectDetails" : [ { "TechnologyUsed" : "Java" }, { "TechnologyUsed" : "MySQL in Backend" } ] } ] }
广告