
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to push a computed expression in a $group?
To push a computed expression in $group, use aggregate. Let us first create a collection with documents −
> db.demo24.insertOne({"Id":100,"Status":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c58722d07d3b95082e72") } > db.demo24.insertOne({"Id":100,"Status":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c58a22d07d3b95082e73") } > db.demo24.insertOne({"Id":100,"Status":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c58f22d07d3b95082e74") } > db.demo24.insertOne({"Id":100,"Status":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c59122d07d3b95082e75") } > db.demo24.insertOne({"Id":100,"Status":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e14c59222d07d3b95082e76") }
Display all documents from a collection with the help of find() method −
> db.demo24.find();
This will produce the following output −
{ "_id" : ObjectId("5e14c58722d07d3b95082e72"), "Id" : 100, "Status" : true } { "_id" : ObjectId("5e14c58a22d07d3b95082e73"), "Id" : 100, "Status" : true } { "_id" : ObjectId("5e14c58f22d07d3b95082e74"), "Id" : 100, "Status" : false } { "_id" : ObjectId("5e14c59122d07d3b95082e75"), "Id" : 100, "Status" : true } { "_id" : ObjectId("5e14c59222d07d3b95082e76"), "Id" : 100, "Status" : false }
Here is the query to push (using $push) a computed expression in a $group −
> db.demo24.aggregate({$group: {_id:'$Id', AllValues: {$push: {$cond: [{$eq: ['$Status', true]},'Active','InActive']}}}})
This will produce the following output −
{ "_id" : 100, "AllValues" : [ "Active", "Active", "InActive", "Active", "InActive" ] }
- Related Articles
- MongoDB query to push document into an array
- MongoDB query to update an array element matching a condition using $push?
- Push query results into variable with MongoDB?
- MongoDB query to group duplicate documents
- MongoDB query to group by _id
- Perform group and distinct together in a single MongoDB query
- A single MongoDB query to orderby date and group by user
- Group query upon nested object in MongoDB?
- Query an array of embedded documents in MongoDB and push another?
- Sort and Group in one MongoDB aggregation query?
- MongoDB query to group records and display a specific value with dot notation
- MongoDB query to group several fields using aggregation framework?
- MongoDB aggregation framework with group query example?
- How to push an array in MongoDB?
- MongoDB Group query to get the count of repeated marks in documents?

Advertisements