
- 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 count the number of array items in documents and display in a new field
To count the number of array items in a document, use $size in MongoDB. Let us create a collection with documents −
> db.demo703.insertOne({"ListOfSubject":["MySQL","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebaf551299a9f98c93b4") } > db.demo703.insertOne({"ListOfSubject":["Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebb5551299a9f98c93b5") } > db.demo703.insertOne({"ListOfSubject":["C","C++","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6ebbf551299a9f98c93b6") }
Display all documents from a collection with the help of find() method −
> db.demo703.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6ebaf551299a9f98c93b4"), "ListOfSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5ea6ebb5551299a9f98c93b5"), "ListOfSubject" : [ "Java" ] } { "_id" : ObjectId("5ea6ebbf551299a9f98c93b6"), "ListOfSubject" : [ "C", "C++", "Python" ] }
Following is the query to count the number of array items −
>db.demo703.aggregate({$project:{NumberOfItemsInEachArray:{$size:"$ListOfSubject"}}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5ea6ebaf551299a9f98c93b4"), "NumberOfItemsInEachArray" : 2 } { "_id" : ObjectId("5ea6ebb5551299a9f98c93b5"), "NumberOfItemsInEachArray" : 1 } { "_id" : ObjectId("5ea6ebbf551299a9f98c93b6"), "NumberOfItemsInEachArray" : 3 }
- Related Articles
- “Structured” grouping query in MongoDB to display result with a new field displaying the count
- MongoDB query to create new field and count set the count of another field in it?
- Get count of array elements from a specific field in MongoDB documents?
- Count the number of items in an array in MongoDB?
- Count unique items in array-based fields across all MongoDB documents?
- MongoDB query to match documents that contain an array field
- MongoDB query to display alternative documents with mapReduce() function and emit even field values
- MongoDB query to add up the values of a specific field in documents
- How to count the number of documents in a MongoDB collection?
- Count number of documents from MongoDB collection inside Array?
- How to add a new field to all the documents in a MongoDB collection
- MongoDB Group query to get the count of repeated marks in documents?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to collectively match intersection of documents against a field
- MongoDB inverse of query to return all items except specific documents?

Advertisements