
- 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
How to count items in array with MongoDB?
To count items in array, use length. Let us create a collection with documents −
> db.demo440.insertOne( ... { ... "Name":"Chris", ... "ListOfFriends":["John","Sam","Mike"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb5") } > > db.demo440.insertOne( ... { ... "Name":"David", ... "ListOfFriends":["Mike","Bob","Carol"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb6") }
Display all documents from a collection with the help of find() method −
> db.demo440.find();
This will produce the following output −
{ "_id" : ObjectId("5e78c63cbbc41e36cc3caeb5"), "Name" : "Chris", "ListOfFriends" : [ "John", "Sam", "Mike" ] } { "_id" : ObjectId("5e78c63cbbc41e36cc3caeb6"), "Name" : "David", "ListOfFriends" : [ "Mike", "Bob", "Carol" ] }
Following is the query to count items in array −
>db.demo440.findOne({'Name':'David'}).ListOfFriends.length;
This will produce the following output −
3
- Related Articles
- Count the number of items in an array in MongoDB?
- Count unique items in array-based fields across all MongoDB documents?
- Grouping the array items in MongoDB and get the count the products with similar price?
- Implement $dateToString on array items with MongoDB
- How to get items from an object array in MongoDB?
- How to group nested fields in MongoDB aggregation with count value in array?
- MongoDB query to count the number of array items in documents and display in a new field
- Count number of elements in an array with MongoDB?
- Clearing items in a nested MongoDB array?
- MongoDB find by multiple array items?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB find by multiple array items using $in?
- How to query all items in MongoDB?
- How to push new items to an array inside of an object in MongoDB?
- How to maintain the top count of array elements in MongoDB?

Advertisements