- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Query to implement $in in array
Let us create a collection with documents −
> db.demo520.insertOne({"ListOfName":["John","Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fb4b3fbf26334ef6114") } > db.demo520.insertOne({"ListOfName":["Chris","David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fbfb3fbf26334ef6115") } > db.demo520.insertOne({"ListOfName":["Mike","Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fc7b3fbf26334ef6116") }
Display all documents from a collection with the help of find() method −
> db.demo520.find();
This will produce the following output −
{ "_id" : ObjectId("5e899fb4b3fbf26334ef6114"), "ListOfName" : [ "John", "Bob" ] } { "_id" : ObjectId("5e899fbfb3fbf26334ef6115"), "ListOfName" : [ "Chris", "David" ] } { "_id" : ObjectId("5e899fc7b3fbf26334ef6116"), "ListOfName" : [ "Mike", "Bob" ] }
Here is the query to implement $in array>
> db.demo520.find({"ListOfName":{$in:["Bob","Mike"]}});
This will produce the following output −
{ "_id" : ObjectId("5e899fb4b3fbf26334ef6114"), "ListOfName" : [ "John", "Bob" ] } { "_id" : ObjectId("5e899fc7b3fbf26334ef6116"), "ListOfName" : [ "Mike", "Bob" ] }
- Related Articles
- MongoDB query to implement OR operator in find()
- How to implement MongoDB $or query?
- MongoDB query to implement aggregate function
- Implement array match in MongoDB?
- Implement MongoDB $push in embedded document array?
- MongoDB query to replace value in an array?
- Implement a query similar to MySQL Union with MongoDB?
- MongoDB query with $all in array
- Query array of subdocuments in MongoDB
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query to fetch array values
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to add new array element in document
- MongoDB query to access an object in an array

Advertisements