在MongoDB的双嵌套数组中搜索文本?
借助dot(。)表示法在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" } ] } ] }