
- 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
How to search documents through an integer array in MongoDB?
You can use $where operator for this. Let us first create a collection with documents −
>db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"John","StudentScores":[45,78,89,90]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a219345990cee87fd88c") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"Larry","StudentScores":[45,43,34,33]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a22a345990cee87fd88d") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"Chris","StudentScores":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a23c345990cee87fd88e") } >db.searchDocumentArrayIntegerDemo.insertOne({"StudentFirstName":"David","StudentScores":[99]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a24d345990cee87fd88f") }
Following is the query to display all documents from a collection with the help of find() method −
> db.searchDocumentArrayIntegerDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2a219345990cee87fd88c"), "StudentFirstName" : "John", "StudentScores" : [ 45, 78, 89, 90 ] } { "_id" : ObjectId("5cd2a22a345990cee87fd88d"), "StudentFirstName" : "Larry", "StudentScores" : [ 45, 43, 34, 33 ] } { "_id" : ObjectId("5cd2a23c345990cee87fd88e"), "StudentFirstName" : "Chris", "StudentScores" : [ ] } { "_id" : ObjectId("5cd2a24d345990cee87fd88f"), "StudentFirstName" : "David", "StudentScores" : [ 99 ] }
Case 1 − Query when array contains at least one value −
> db.searchDocumentArrayIntegerDemo.find({ $where: "this.StudentScores.length >= 1" } );
This will produce the following output −
{ "_id" : ObjectId("5cd2a219345990cee87fd88c"), "StudentFirstName" : "John", "StudentScores" : [ 45, 78, 89, 90 ] } { "_id" : ObjectId("5cd2a22a345990cee87fd88d"), "StudentFirstName" : "Larry", "StudentScores" : [ 45, 43, 34, 33 ] } { "_id" : ObjectId("5cd2a24d345990cee87fd88f"), "StudentFirstName" : "David", "StudentScores" : [ 99 ] }
Case 2 − Query when array contains a common value in all documents −
> db.searchDocumentArrayIntegerDemo.find({StudentScores: 45}, {StudentScores:1});
This will produce the following output −
{ "_id" : ObjectId("5cd2a219345990cee87fd88c"), "StudentScores" : [ 45, 78, 89, 90 ] } { "_id" : ObjectId("5cd2a22a345990cee87fd88d"), "StudentScores" : [ 45, 43, 34, 33 ] }
- Related Articles
- Search for documents matching first item in an array with MongoDB?
- How to filter documents based on an array in MongoDB?
- Search for multiple documents in MongoDB?
- How to aggregate array documents in MongoDB?
- How to use MongoDB $pull to delete documents within an Array?
- How to insert an 8-byte integer into MongoDB through JavaScript shell?
- Search an array of hashes in MongoDB?
- MongoDB query to match documents that contain an array field
- MongoDB Regex Search on Integer Value?
- How to search in array of object in MongoDB?
- MongoDB query to find matching documents given an array with values?
- MongoDB aggregation to sum individual properties on an object in an array across documents
- Can we search an array of objects in MongoDB?
- How to find latest entries in array over all MongoDB documents?
- How can I concatenate an array of integer in MongoDB aggregation method?

Advertisements