Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to project grouping into object in MongoDB and display only the marks field?
Let us first create a document −
> var document= [
... { "SubjectName" : "MySQL", "Marks" : 78 },
... { "SubjectName" : "MongoDB", "Marks" : 89 },
... { "SubjectName" : "Java", "Marks" : 71 },
... ];
Following is the query to display document −
> printjson(document);
This will produce the following output −
[
{
"SubjectName" : "MySQL",
"Marks" : 78
},
{
"SubjectName" : "MongoDB",
"Marks" : 89
},
{
"SubjectName" : "Java",
"Marks" : 71
}
]
Following is the query to project grouping into an object in MongoDB −
> var makeObject= {};
> document.forEach(function (d){
... makeObject[d.SubjectName] = d.Marks;
... });
> printjson(makeObject);
This will produce the following output −
{ "MySQL" : 78, "MongoDB" : 89, "Java" : 71 }Advertisements