要获取JSON数据的内部值,请find()
与dot(。)表示法一起使用。让我们创建一个包含文档的集合-
> db.demo235.insertOne( ... { ... "id":101, ... "details":[ ... { ... "Name":"Chris Brown", ... "Age":21 ... }, ... { ... "Name":"David Miller", ... "Age":24 ... } ... ], ... "otherdetails":[ ... { ... "Score":56, ... "Subject":"MongoDB" ... }, ... { ... "Score":78, ... "Subject":"MySQL" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e418d22f4cebbeaebec514b") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo235.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e418d22f4cebbeaebec514b"), "id" : 101, "details" : [ { "Name" : "Chris Brown", "Age" : 21 }, { "Name" : "David Miller", "Age" : 24 } ], "otherdetails" : [ { "Score" : 56, "Subject" : "MongoDB" }, { "Score" : 78, "Subject" : "MySQL" } ] }
以下是获取json数据的内部值的查询-
> db.demo235.find({},{"otherdetails.Subject":1,_id:0});
这将产生以下输出-
{ "otherdetails" : [ { "Subject" : "MongoDB" }, { "Subject" : "MySQL" } ] }