
- 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
In MongoDB, is using $in search faster than multiple single searches?
Yes, using $in is faster. Let us see an example and create a collection with documents −
> db.demo653.insertOne({subject:"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b274deddd72997713c0") } > db.demo653.insertOne({subject:"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b304deddd72997713c1") } > db.demo653.insertOne({subject:"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b354deddd72997713c2") } > db.demo653.insertOne({subject:"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b384deddd72997713c3") } > db.demo653.insertOne({subject:"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea04b3b4deddd72997713c4") }
Display all documents from a collection with the help of find() method −
> db.demo653.find();
This will produce the following output −
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" } { "_id" : ObjectId("5ea04b304deddd72997713c1"), "subject" : "MongoDB" } { "_id" : ObjectId("5ea04b354deddd72997713c2"), "subject" : "Java" } { "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" } { "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" }
Following is the query to use $in and search faster than multiple single searches −
> db.demo653.find({subject:{$in:["MySQL","C++","C"]}});
This will produce the following output −
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" } { "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" } { "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" }
- Related Articles
- Search multiple fields for multiple values in MongoDB?
- Search for multiple documents in MongoDB?
- Update multiple rows in a single MongoDB query?
- Is SELECT * faster than 40 columns listing in MySQL?
- Do you think operator < is faster than
- Using a regex with text search in MongoDB
- Using find() to search for nested keys in MongoDB?
- Why does light travel faster than sound like we see lightning faster than thunder?
- MongoDB find by multiple array items using $in?
- Multiple atomic updates using MongoDB?
- A single MySQL query to search multiple words from different column values
- Google’s Top Technology Searches in 2015
- Why is the formation of curd faster in summer than that in winter?
- How to delete multiple documents in MongoDB using deleteMany()?
- Create multiple regression lines in a single plot using ggplot2 in R.

Advertisements