
- 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
Get the size of a collection in MongoDB using conditions?
Let us first create a collection with documents −
> dbmongoDBCollectionSizeDemoinsertOne({"Name":"John", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e3db64a577be5a2bc16") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Chris", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e45b64a577be5a2bc17") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Robert", "Age":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e4db64a577be5a2bc18") }
Following is the query to display all documents from a collection with the help of find() method −
> dbmongoDBCollectionSizeDemofind();
This will produce the following document −
{ "_id" : ObjectId("5cf23e3db64a577be5a2bc16"), "Name" : "John", "Age" : 23 } { "_id" : ObjectId("5cf23e45b64a577be5a2bc17"), "Name" : "Chris", "Age" : 24 } { "_id" : ObjectId("5cf23e4db64a577be5a2bc18"), "Name" : "Robert", "Age" : 26 }
Following is the query to get the collection size using conditions −
> var collectionsize = 0; > dbmongoDBCollectionSizeDemofind({Name:'Robert'})forEach(function(d) { var s = Objectbsonsize(d); collectionsize=collectionsize +s; }) > print(collectionsize);
This will produce the following document −
52
- Related Articles
- Delete data from a collection in MongoDB using multiple conditions?
- Get the storage size of a database in MongoDB?
- Java Program to Get the Size of the Collection
- Get the count of the number of documents in a MongoDB Collection?
- Get the maximum element in MongoDB collection?
- Get names of all keys in the MongoDB collection?
- Find the MongoDB collection size for name “Chris”
- Get execution stats in MongoDB for a collection
- Get all fields names in a MongoDB collection?
- Get the size of all the documents in a MongoDB query?
- What is the MongoDB Capped Collection maximum allowable size?
- Get the top most document from a MongoDB collection
- How to get the child of a MongoDB collection by the key?
- How to get array from a MongoDB collection?
- Get database data size in MongoDB?

Advertisements