
- 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
Native Querying MongoDB inside array and get the count
To query inside array and check for existence to get the count, use $exists. Let us create a collection with documents −
> db.demo296.insertOne( ... { ... "id":101, ... "Name":"Chris", ... "details":[ ... { ... SubjectId:[101,103], ... "SubjectName":["MySQL","MongoDB"] ... }, ... { ... SubjectId:[102,104], ... "SubjectName":["Java","C"] ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d51715d93261e4bc9ea3b") } > db.demo296.insertOne( ...{ ... "id":102, ... "Name":"David", ... "details":[ ... { ... SubjectId:[110,113] ... ... }, ... { ... SubjectId:[112,114] ... ... } ... ] ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d519b5d93261e4bc9ea3c") } > db.demo296.insertOne( ... { ... "id":103, ... "Name":"Bob", ... "details":[ ... { ... "SubjectName":["C++","Python"] ... }, ... { ... "SubjectName":["Spring","Hibernate"] ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d52315d93261e4bc9ea3d") }
Display all documents from a collection with the help of find() method −
> db.demo296.find();
This will produce the following output −
{ "_id" : ObjectId("5e4d51715d93261e4bc9ea3b"), "id" : 101, "Name" : "Chris", "details" : [ { "SubjectId" : [ 101, 103 ], "SubjectName" : [ "MySQL", "MongoDB" ] }, { "SubjectId" : [ 102, 104 ], "SubjectName" : [ "Java", "C" ] } ] } { "_id" : ObjectId("5e4d519b5d93261e4bc9ea3c"), "id" : 102, "Name" : "David", "details" : [ { "SubjectId" : [ 110, 113 ] }, { "SubjectId" : [ 112, 114 ] } ] } { "_id" : ObjectId("5e4d52315d93261e4bc9ea3d"), "id" : 103, "Name" : "Bob", "details" : [ { "SubjectName" : [ "C++", "Python" ] }, { "SubjectName" : [ "Spring", "Hibernate" ] } ] }
Following is how to query MongoDB inside arrays and get the count −
> db.demo296.count( { 'details.SubjectName': {$exists: true }} );
This will produce the following output −
2
- Related Articles
- Querying array elements with MongoDB?
- Get array items inside a MongoDB document?
- Querying internal array size in MongoDB?
- Count number of documents from MongoDB collection inside Array?
- Querying an array of arrays in MongoDB?
- How to get the matching document inside an array in MongoDB?
- Querying object's field array values in MongoDB?
- Get at least one match in list querying with MongoDB?
- Grouping the array items in MongoDB and get the count the products with similar price?
- Querying with MongoDB subelement?
- Querying array of Embedded Documents in MongoDB based on Range?
- Querying from part of object in an array with MongoDB
- MongoDB Aggregation to slice array inside array
- MongoDB $unwind to get the count
- Querying null value in MongoDB?

Advertisements