
- 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
Querying internal array size in MongoDB?
You can use the $size operator for internal array size in MongoDB. The syntax is as follows:
db.internalArraySizeDemo.aggregate( [ { $group: { _id:yourObjectIdValue, anyFieldName: {$first: {$size: "$yourArrayName" }} } } ] );
To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents are as follows:
>db.internalArraySizeDemo.insertOne({"EmployeeName":"Mike","EmployeeTechnology":["Jav a Web Development","Python Web Development"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eff586fd07954a48906b2") } > db.internalArraySizeDemo.insertOne({"EmployeeName":"Sam","EmployeeTechnology":["C with Graphics","Game Development with C++ Language","MatLab"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6effcd6fd07954a48906b3") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.internalArraySizeDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6eff586fd07954a48906b2"), "EmployeeName" : "Mike", "EmployeeTechnology" : [ "Java Web Development", "Python Web Development" ] } { "_id" : ObjectId("5c6effcd6fd07954a48906b3"), "EmployeeName" : "Sam", "EmployeeTechnology" : [ "C with Graphics", "Game Development with C++ Language", "MatLab" ] }
Here is the query to get the internal array size with the help of $size operator:
> db.internalArraySizeDemo.aggregate( ... [ ... { ... $group: { ... _id:ObjectId("5c6eff586fd07954a48906b2"), ... EmployeeTechnology_count: {$first: {$size: "$EmployeeTechnology" }} ... } ... } ... ] ... );
The following is the output:
{ "_id" : ObjectId("5c6eff586fd07954a48906b2"), "EmployeeTechnology_count" : 2 }
- Related Articles
- Querying array elements with MongoDB?
- Querying an array of arrays in MongoDB?
- Querying object's field array values in MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- Querying from part of object in an array with MongoDB
- Native Querying MongoDB inside array and get the count
- Querying null value in MongoDB?
- Querying with MongoDB subelement?
- How to improve querying field in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- Fetch records in MongoDB on querying its subset
- Best way to sum array size fields in MongoDB?
- Get at least one match in list querying with MongoDB?
- How to use arrays as filters by querying subdocuments in MongoDB?
- Querying only the field name and display only the id in MongoDB?

Advertisements