

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MongoDB query where all array items are greater than a specified condition?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query for documents matching array, irrespective of elements order
- MongoDB query where all array items are less than a specified condition?
- How to select where sum of fields is greater than a value in MongoDB?
- MySQL GROUP BY with WHERE clause and condition count greater than 1?
- Write a MySQL query where length of the records is longer than 1?
- MongoDB - Query embedded documents?
- Get the size of all the documents in a MongoDB query?
- MongoDB query for array concatenation?
- MongoDB query to skip documents
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Find MongoDB documents where all objects in array have specific value?
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to get specific list of names from documents where the value of a field is an array
Advertisements