
- 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
Get at least one match in list querying with MongoDB?
Use $in operator to get at least one match. Let us first create a collection with documents −
> db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["MySQL","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db5db64f4b851c3a13ce") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Java","C","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db71b64f4b851c3a13cf") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Python","C++","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db87b64f4b851c3a13d0") } >db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Ruby","Javascript","C#","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dba9b64f4b851c3a13d1") }
Following is the query to display all documents from a collection with the help of find() method −
> db.atleastOneMatchDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2db5db64f4b851c3a13ce"), "StudentFavouriteSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5cd2db71b64f4b851c3a13cf"), "StudentFavouriteSubject" : [ "Java", "C", "MongoDB" ] } { "_id" : ObjectId("5cd2db87b64f4b851c3a13d0"), "StudentFavouriteSubject" : [ "Python", "C++", "SQL Server" ] } { "_id" : ObjectId("5cd2dba9b64f4b851c3a13d1"), "StudentFavouriteSubject" : [ "Ruby", "Javascript", "C#", "MySQL" ] }
Following is the query to get at least one match −
>db.atleastOneMatchDemo.find({"StudentFavouriteSubject":{"$in":["MongoDB","MySQL"]}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2db5db64f4b851c3a13ce"), "StudentFavouriteSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5cd2db71b64f4b851c3a13cf"), "StudentFavouriteSubject" : [ "Java", "C", "MongoDB" ] } { "_id" : ObjectId("5cd2dba9b64f4b851c3a13d1"), "StudentFavouriteSubject" : [ "Ruby", "Javascript", "C#", "MySQL" ] }
- Related Articles
- Querying with MongoDB subelement?
- Querying array elements with MongoDB?
- Native Querying MongoDB inside array and get the count
- How to aggregate two lists if at least one element matches in MongoDB?
- MongoDB aggregation to get two documents with the least marks
- Querying null value in MongoDB?
- Convert inputs to arrays with at least one dimension in Numpy
- How to match date with MongoDB $match?
- Querying from part of object in an array with MongoDB
- Querying internal array size in MongoDB?
- Find document in MongoDB where at least one item from an array is not in the other?
- Program to check every sublist in a list containing at least one unique element in Python
- Querying an array of arrays in MongoDB?
- How to improve querying field in MongoDB?
- Get at least x number of rows in MySQL?

Advertisements