
- 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
Search multiple fields for multiple values in MongoDB?
To search multiple fields for multiple values in MongoDB, you can use $text and $search operator. Let us first create a collection with documents
>db.searchMultipleFieldsDemo.insertOne({"_id":100,"FirstSubject":"Java","SecondSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : 100 } >db.searchMultipleFieldsDemo.insertOne({"_id":101,"FirstSubject":"MongoDB","SecondSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : 101 } >db.searchMultipleFieldsDemo.insertOne({"_id":102,"FirstSubject":"MySQL","SecondSubject":"Java"}); { "acknowledged" : true, "insertedId" : 102 }
Following is the query to display all documents from a collection with the help of find() method
> db.searchMultipleFieldsDemo.find().pretty();
This will produce the following output
{ "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" } { "_id" : 102, "FirstSubject" : "MySQL", "SecondSubject" : "Java" }
Following is the query to search multiple fields for multiple values in MongoDB
> db.searchMultipleFieldsDemo.find({"$text":{"$search":"Java MongoDB"}});
This will produce the following output
{ "_id" : 102, "FirstSubject" : "MySQL", "SecondSubject" : "Java" } { "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" }
- Related Articles
- Search for multiple documents in MongoDB?
- MongoDB query for exact match on multiple document fields
- Performing distinct on multiple fields in MongoDB?
- How do I index “or” in MongoDB for indexing multiple fields?
- Count by multiple fields with MongoDB aggregation
- Prevent duplicates of multiple fields with index in MongoDB
- MongoDB query to check the existence of multiple fields
- Query for multiple parameters in MongoDB?
- Select multiple values with MongoDB OR operator
- In MongoDB, is using $in search faster than multiple single searches?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- MongoDB query to pull multiple values from array
- How to fetch fields with multiple values set using MySQL LIKE?
- JavaScript Array: Checking for multiple values
- Update values in multiple documents with multi parameter in MongoDB?

Advertisements