
- 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 query on list field in MongoDB?
To understand the query on list field, and/or, you can create a collection with documents.
The query to create a collection with a document is as follows −
> db.andOrDemo.insertOne({"StudentName":"Larry","StudentScore":[33,40,50,60,70]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9522d316f542d757e2b444") } > db.andOrDemo.insertOne({"StudentName":"Larry","StudentScore":[87,67,79,98,90]}); { "acknowledged" : true, "insertedId" : ObjectId("5c95230916f542d757e2b445") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.andOrDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c9522d316f542d757e2b444"), "StudentName" : "Larry", "StudentScore" : [ 33, 40, 50, 60, 70 ] } { "_id" : ObjectId("5c95230916f542d757e2b445"), "StudentName" : "Larry", "StudentScore" : [ 87, 67, 79, 98, 90 ] }
Here is the query on list field.
The query is as follows −
> db.andOrDemo.find({"StudentScore":70}).pretty();
The following is the output:
{ "_id" : ObjectId("5c9522d316f542d757e2b444"), "StudentName" : "Larry", "StudentScore" : [ 33, 40, 50, 60, 70 ] }
Case 3 − Here is the query for or list field.
The query is as follows −
> db.andOrDemo.find({"$or":[ {"StudentScore":60}, {"StudentScore":90}]}).pretty();
Sample Output −
{ "_id" : ObjectId("5c9522d316f542d757e2b444"), "StudentName" : "Larry", "StudentScore" : [ 33, 40, 50, 60, 70 ] } { "_id" : ObjectId("5c95230916f542d757e2b445"), "StudentName" : "Larry", "StudentScore" : [ 87, 67, 79, 98, 90 ] }
- Related Articles
- How to trim spaces from field in MongoDB query?
- MongoDB query to find on field combination of FirstName and LastName?
- MongoDB query to fetch only the “Name” field based on roles?
- MongoDB query by sub-field?
- How to get tag count in MongoDB query results based on list of names?
- MongoDB query for a single field
- MongoDB query to update array with another field?
- How to query on top N rows in MongoDB?
- How to add a field with static value to MongoDB find query?
- How to run MongoDB query to update only a specific field value?
- Indexing large text field to make query faster in MongoDB
- MongoDB query to add matched key to list after query?
- MongoDB query to change simple field into an object?
- How to get distinct list of sub-document field values in MongoDB?
- How to fire find query on sub-documents in MongoDB?

Advertisements