- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to group records and display a specific value with dot notation
Let us create a collection with documents −
> db.demo430.insertOne( ... { ... "details": [ ... { ... "Name":"Chris" ... } , ... {"Name":"David"}, ... {"Name":"Adam"}, ... {"Name":"Bob"} ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e7702ddbbc41e36cc3cae88") }
Display all documents from a collection with the help of find() method −
> db.demo430.find();
This will produce the following output −
{ "_id" : ObjectId("5e7702ddbbc41e36cc3cae88"), "details" : [ { "Name" : "Chris" }, { "Name" : "David" }, { "Name" : "Adam" }, { "Name" : "Bob" } ] }
Following is the query to group records −
> db.demo430.aggregate([{ "$group" : { "_id" : {"Name" : "$details.Name"}}}]);
This will produce the following output −
{ "_id" : { "Name" : [ "Chris", "David", "Adam", "Bob" ] } }
- Related Articles
- Field selection within MongoDB query using dot notation?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Find MongoDB records with Price less than a specific value
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB Query to search for records only in a specific hour?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to 'sort' and display a specific number of values
- Display only the employee names with specific salaries in MongoDB documents with employee records?
- MongoDB query to pull a specific value from a document
- MongoDB query select and display only a specific field from the document?
- MongoDB query to match documents with array values greater than a specific value
- MySQL query to subtract date records with week day and display the weekday with records
- MongoDB query to increment a specific value using custom variable
- MongoDB query (aggregation framework) to match a specific field value

Advertisements