
- 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 create new field and count set the count of another field in it?
For new field, use $addFields in MongoDB. The $addFields is used to add new fields to documents. Let us create a collection with documents −
> db.demo429.insertOne( ... { ... "_id": 101, ... "Value": 3, ... "details": [ ... { ... "Age": 29, ... "Value": 3, ... "details1": [ ... 1, ... 2, ... 3 ... ] ... }, ... { ... "Age": 31, ... "Value": 4, ... "details1": [ ... 354 ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo429.find();
This will produce the following output −
{ "_id" : 101, "Value" : 3, "details" : [ { "Age" : 29, "Value" : 3, "details1" : [ 1, 2, 3 ] }, { "Age" : 31, "Value" : 4, "details1" : [ 354 ] } ] }
Following is the query to create new field and set the count of other field −
> db.demo429.aggregate([ ... { ... "$addFields": { ... "details" : { ... "$map": { ... "input": "$details", ... "as": "d", ... "in": { ... "Age" : "$$d.Age", ... "NumberOfDetails1": { "$size": "$$d.details1" } ... } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : 101, "Value" : 3, "details" : [ { "Age" : 29, "NumberOfDetails1" : 3 }, { "Age" : 31, "NumberOfDetails1" : 1 } ] }
- Related Articles
- MongoDB query to count the number of array items in documents and display in a new field
- “Structured” grouping query in MongoDB to display result with a new field displaying the count
- MongoDB query to update array with another field?
- MySQL query to count comma’s from field value?
- MySQL query to get the count of all the elements in the field?
- Update MongoDB field using value of another field?
- How to count and sum a field between 2 dates in MongoDB?
- How to count number of distinct values per field/ key in MongoDB?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- Count boolean field values within a single MySQL query?
- MongoDB query to add a new field and concatenate the price result divided by a specific number in it
- How can I aggregate collection and group by field count in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB query to convert the field value and create datetime day of month during projection?
- MongoDB query for counting number of unique fields grouped by another field?

Advertisements