
- 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 query to find matching documents given an array with values?
For specific documents, use MongoDB $in. Let us create a collection with documents −
> db.demo511.insertOne({"ListOfProject":["Library Management System","Hospital Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e88473a987b6e0e9d18f585") } > db.demo511.insertOne({"ListOfProject":["Online Web Tracking","Library Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e884751987b6e0e9d18f586") } > db.demo511.insertOne({"ListOfProject":["Online Shopping Cart","Hospital Management System"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e884776987b6e0e9d18f587") }
Display all documents from a collection with the help of find() method −
> db.demo511.find();
This will produce the following output −
{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ObjectId("5e884751987b6e0e9d18f586"), "ListOfProject" : [ "Online Web Tracking", "Library Management System" ] } { "_id" : ObjectId("5e884776987b6e0e9d18f587"), "ListOfProject" : [ "Online Shopping Cart", "Hospital Management System" ] }
Following is the query to finding matching documents −
> db.demo511.find({ "ListOfProject":{$in:["Hospital Management System","Online Shopping Cart"]}});
This will produce the following output −
{ "_id" : ObjectId("5e88473a987b6e0e9d18f585"), "ListOfProject" : [ "Library Management System", "Hospital Management System" ] } { "_id" : ObjectId("5e884776987b6e0e9d18f587"), "ListOfProject" : [ "Online Shopping Cart", "Hospital Management System" ] }
- Related Articles
- MongoDB query for documents matching array, irrespective of elements order
- Search for documents matching first item in an array with MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- MongoDB query to update all documents matching specific IDs
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to match documents that contain an array field
- MongoDB query to convert an array to a map of documents with n attributes?
- MongoDB query to update an array element matching a condition using $push?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query to get distinct FirstName values from documents
- How to find documents with exactly the same array entries as in a MongoDB query?
- How to specify the order in which the query returns matching documents in MongoDB
- Query an array of embedded documents in MongoDB and push another?
- MongoDB query to fetch array values
- Match MongoDB documents with fields not containing values in array?

Advertisements