
- 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
MongoDB equivalent of WHERE IN(1,2,…)?
The MongoDB equivalent of WHERE IN(1,2,....) is $in operator. The syntax is as follows
db.yourCollectionName.find({yourFieldName:{$in:[yourValue1,yourValue2,....N]}}).pretty();
Let us first create a collection with documents
> db.whereInDemo.insertOne({"StudentName":"John","StudentMathScore":57}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281ec6304881c5ce84ba5") } > db.whereInDemo.insertOne({"StudentName":"Larry","StudentMathScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281f56304881c5ce84ba6") } > db.whereInDemo.insertOne({"StudentName":"Chris","StudentMathScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281fd6304881c5ce84ba7") } > db.whereInDemo.insertOne({"StudentName":"Robert","StudentMathScore":99}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2820a6304881c5ce84ba8") } > db.whereInDemo.insertOne({"StudentName":"Bob","StudentMathScore":97}); { "acknowledged" : true, "insertedId" : ObjectId("5ca282206304881c5ce84ba9") }
Following is the query to display all documents from a collection with the help of find() method
> db.whereInDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca281ec6304881c5ce84ba5"), "StudentName" : "John", "StudentMathScore" : 57 } { "_id" : ObjectId("5ca281f56304881c5ce84ba6"), "StudentName" : "Larry", "StudentMathScore" : 89 } { "_id" : ObjectId("5ca281fd6304881c5ce84ba7"), "StudentName" : "Chris", "StudentMathScore" : 98 } { "_id" : ObjectId("5ca2820a6304881c5ce84ba8"), "StudentName" : "Robert", "StudentMathScore" : 99 } { "_id" : ObjectId("5ca282206304881c5ce84ba9"), "StudentName" : "Bob", "StudentMathScore" : 97 }
Following is the query that represents the equivalent of where in MongoDB with $in operator:
> db.whereInDemo.find({StudentMathScore:{$in:[97,98,99]}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5ca281fd6304881c5ce84ba7"), "StudentName" : "Chris", "StudentMathScore" : 98 } { "_id" : ObjectId("5ca2820a6304881c5ce84ba8"), "StudentName" : "Robert", "StudentMathScore" : 99 } { "_id" : ObjectId("5ca282206304881c5ce84ba9"), "StudentName" : "Bob", "StudentMathScore" : 97 }
- Related Articles
- MongoDB equivalent of `select distinct(name) from collectionName where age = “25”`?
- MongoDB equivalent of SELECT field AS `anothername`?
- What is the equivalent of SQL “like” in MongoDB?
- Why is [1,2] + [3,4] = “1,23,4” in JavaScript?
- Using count equivalent in MongoDB to find top users with max occurrence
- How to get the equivalent for SELECT column1, column2 FROM tbl in MongoDB Database?
- How to select where sum of fields is greater than a value in MongoDB?
- Update key value where another key equals some value in MongoDB?
- Update key value where different key equals some value in MongoDB?
- Find MongoDB documents where all objects in array have specific value?
- Find MongoDB documents where elements of an array have a specific value?
- Find MongoDB records where array field is not empty?
- Find documents where all elements of an array have a specific value in MongoDB?\n
- Query for documents where array size is greater than 1 in MongoDB?
- C++ equivalent of instanceof

Advertisements