

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 }
- Related Questions & Answers
- Querying only the field name and display only the id in MongoDB?
- Project field in MongoDB
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- MongoDB query select and display only a specific field from the document?
- MongoDB query to change simple field into an object?
- How can I sort documents in MongoDB 4 and display only a single field?
- Finding average marks of students for different subjects and display only the highest average marks in MySQL
- “Structured” grouping query in MongoDB to display result with a new field displaying the count
- How to project specific elements in an array field with MongoDB?
- Project specific array field in a MongoDB collection?
- Display only a single field from all the documents in a MongoDB collection
- How to display only the keys from nested MongoDB documents?
- How to return only value of a field in MongoDB?
- How to apply a condition only if field exists in MongoDB?
- How can I display only unique record from MongoDB and ignore the duplicates?
Advertisements