使用点表示法访问对象。首先让我们创建一个包含文档的集合
> db.nestedObjectDemo.insertOne({"Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], ... "StudentCountryName" : [ "US" ], ... "StudentCoreSubject" : [ "C", "Java" ], ... "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }); { "acknowledged" : true, "insertedId" : ObjectId("5c99dfc2863d6ffd454bb650") }
以下是在find()
方法的帮助下显示集合中所有文档的查询
> db.nestedObjectDemo.find().pretty();
这将产生以下输出
{ "_id" : ObjectId("5c99dfc2863d6ffd454bb650"), "Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], "StudentCountryName" : [ "US" ], "StudentCoreSubject" : [ "C", "Java" ], "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }
以下是使用点表示法访问嵌套对象的查询
>db.nestedObjectDemo.find({"Student.StudentDetails.StudentPersonalDetails.StudentName":"John"}).pretty();
这将产生以下输出
{ "_id" : ObjectId("5c99dfc2863d6ffd454bb650"), "Student" : { "StudentDetails" : { "StudentPersonalDetails" : { "StudentName" : [ "John" ], "StudentCountryName" : [ "US" ], "StudentCoreSubject" : [ "C", "Java" ], "StudentProject" : [ "Online Book Store", "Pig Dice Game" ] } } } }