

- 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
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 Questions & Answers
- 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 null value in MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- Get at least one match in list querying with MongoDB?
- How to improve querying field in MongoDB?
- Count number of elements in an array with MongoDB?
- Fetch a specific document in MongoDB with array elements
- Fetch records in MongoDB on querying its subset
- How to project specific elements in an array field with MongoDB?
Advertisements