让我们首先创建一个文档-
> var document= [ ... { "SubjectName" : "MySQL", "Marks" : 78 }, ... { "SubjectName" : "MongoDB", "Marks" : 89 }, ... { "SubjectName" : "Java", "Marks" : 71 }, ... ];
以下是显示文档的查询-
> printjson(document);
这将产生以下输出-
[ { "SubjectName" : "MySQL", "Marks" : 78 }, { "SubjectName" : "MongoDB", "Marks" : 89 }, { "SubjectName" : "Java", "Marks" : 71 } ]
以下是将项目分组到MongoDB中的对象的查询-
> var makeObject= {}; > document.forEach(function (d){ ... makeObject[d.SubjectName] = d.Marks; ... }); > printjson(makeObject);
这将产生以下输出-
{ "MySQL" : 78, "MongoDB" : 89, "Java" : 71 }