
- 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
Query for documents where array size is greater than 1 in MongoDB?
You can use length to query for documents where array size is greater than 1:
db.yourCollectionName.find({$where:"this.yourArrayDocumentName.length > 1"}).pretty();
To understand the above syntax, let us create a collection with some documents. The query is as follows to create a collection with documents:
>db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Larry","StudentTechnicalSubje ct":["Java","C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c4c0c3d5054b766a76a") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell","StudentTechnicalSu bject":["MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c660c3d5054b766a76b") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell","StudentTechnicalSu bject":["MySQL","SQL Server","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6d6c800c3d5054b766a76c") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.arrayLengthGreaterThanOne.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6d6c4c0c3d5054b766a76a"), "StudentName" : "Larry", "StudentTechnicalSubject" : [ "Java", "C", "C++" ] } { "_id" : ObjectId("5c6d6c660c3d5054b766a76b"), "StudentName" : "Maxwell", "StudentTechnicalSubject" : [ "MongoDB" ] } { "_id" : ObjectId("5c6d6c800c3d5054b766a76c"), "StudentName" : "Maxwell", "StudentTechnicalSubject" : [ "MySQL", "SQL Server", "PL/SQL" ] }
Here is the query for documents where array size is greater than 1. The below query will give all documents where array size is greater than 1:
> db.arrayLengthGreaterThanOne.find({$where:"this.StudentTechnicalSubject.length > 1"}).pretty();
The following is the output:
{ "_id" : ObjectId("5c6d6c4c0c3d5054b766a76a"), "StudentName" : "Larry", "StudentTechnicalSubject" : [ "Java", "C", "C++" ] } { "_id" : ObjectId("5c6d6c800c3d5054b766a76c"), "StudentName" : "Maxwell", "StudentTechnicalSubject" : [ "MySQL", "SQL Server", "PL/SQL" ] }
- Related Articles
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query where all array items are greater than a specified condition?
- MongoDB query for documents matching array, irrespective of elements order
- Get the size of all the documents in a MongoDB query?
- MongoDB query where all array items are less than a specified condition?
- MongoDB - Query embedded documents?
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to find “where” Billing Address is Equal to Delivery Address from documents?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Find MongoDB documents where all objects in array have specific value?
- How to select where sum of fields is greater than a value in MongoDB?
- MongoDB query to get only specific fields in nested array documents?
- Query an array of embedded documents in MongoDB and push another?
- MongoDB query to match documents that contain an array field
- MongoDB query to skip documents

Advertisements