
- 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 array elements with MongoDB?
MongoDB is better when you are querying array elements. Let us use the following syntax for querying array elements −
db.yourCollectionName.find({yourArrayFieldName:"yourValue"}).pretty();
The above syntax will return all those documents which have the value “yourValue” in an array field.
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.queryArrayElementsDemo.insertOne({ ... "StudentName":"John","StudentFavouriteSubject":["C","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90c0354afe5c1d2279d694") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"Carol","StudentFavouriteSubject":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90c0434afe5c1d2279d695") } > db.queryArrayElementsDemo.insertOne({ "StudentName":"David","StudentFavouriteSubject":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90c0584afe5c1d2279d696") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.queryArrayElementsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c90c0354afe5c1d2279d694"), "StudentName" : "John", "StudentFavouriteSubject" : [ "C", "Java" ] } { "_id" : ObjectId("5c90c0434afe5c1d2279d695"), "StudentName" : "Carol", "StudentFavouriteSubject" : [ "C", "C++" ] } { "_id" : ObjectId("5c90c0584afe5c1d2279d696"), "StudentName" : "David", "StudentFavouriteSubject" : [ "MongoDB", "Java" ] }
Querying array elements with MongoDB −
> db.queryArrayElementsDemo.find({StudentFavouriteSubject:"Java"}).pretty();
The following is the output −
{ "_id" : ObjectId("5c90c0354afe5c1d2279d694"), "StudentName" : "John", "StudentFavouriteSubject" : [ "C", "Java" ] } { "_id" : ObjectId("5c90c0584afe5c1d2279d696"), "StudentName" : "David", "StudentFavouriteSubject" : [ "MongoDB", "Java" ] }
Look at the above sample output, the above query returns all those documents with the value “Java”.
- Related Articles
- Querying with MongoDB subelement?
- Querying internal array size in MongoDB?
- Querying from part of object in an array with MongoDB
- Querying an array of arrays in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- Querying object's field array values in MongoDB?
- Native Querying MongoDB inside array and get the count
- Querying array of Embedded Documents in MongoDB based on Range?
- Querying null value in MongoDB?
- Get at least one match in list querying with MongoDB?
- Count number of elements in an array with MongoDB?
- Fetch a specific document in MongoDB with array elements
- How to improve querying field in MongoDB?
- Fetch records in MongoDB on querying its subset
- How to project specific elements in an array field with MongoDB?

Advertisements