
- 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 “$and” operator for subcollection to fetch a document?
To fetch a document, use $in, instead of $and in MongoDB. Let us first create a collection with documents −
> db.demo83.insertOne( ... { ... "Details":[ ... { ... "Name":"Chris", ... "Subject":[ ... "MySQL", ... "MongoDB" ... ] ... }, ... { ... "Name":"David", ... "Subject":[ ... "Java", ... "C" ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfd2271bf0181ecc422a3") } > db.demo83.insertOne( ... { ... "Details":[ ... { ... "Name":"Bob", ... "Subject":[ ... "C++", ... "Python" ... ] ... }, ... { ... "Name":"John", ... "Subject":[ ... "Spring", ... "Hibernate" ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfd4571bf0181ecc422a4") }
Display all documents from a collection with the help of find() method −
> db.demo83.find();
This will produce the following output −
{ "_id" : ObjectId("5e2bfd2271bf0181ecc422a3"), "Details" : [ { "Name" : "Chris", "Subject" : [ "MySQL", "MongoDB" ] }, { "Name" : "David", "Subject" : [ "Java", "C" ] } ] } { "_id" : ObjectId("5e2bfd4571bf0181ecc422a4"), "Details" : [ { "Name" : "Bob", "Subject" : [ "C++", "Python" ] }, { "Name" : "John", "Subject" : [ "Spring", "Hibernate" ] } ] }
Following is the query to find a document using $in −
> db.demo83.find({ "Details" : { "$elemMatch" : {"Name" :"Chris" , "Subject":{"$in":["MongoDB"]}}}});
This will produce the following output −
{ "_id" : ObjectId("5e2bfd2271bf0181ecc422a3"), "Details" : [ { "Name" : "Chris", "Subject" : [ "MySQL", "MongoDB" ] }, { "Name" : "David", "Subject" : [ "Java", "C" ] } ] }
- Related Articles
- MongoDB to fetch documents with $or Operator
- Fetch a specific document in MongoDB with array elements
- Make nested queries in MongoDB 4 to fetch a specific document
- Accessing array values in a MongoDB collection to fetch a specific document
- MongoDB query to fetch a document that does not have a particular field?
- MongoDB query to implement nor query to fetch documents except a specific document
- Update a specific MongoDB document in array with $set and positional $ operator?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Check for Existing Document in MongoDB?
- MongoDB find() query for nested document?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- MongoDB - How to check for equality in collection and in embedded document?
- MongoDB query for fields in embedded document?
- How to update a MongoDB document for adding a new item to an array?
- How to add a sub-document to sub-document array in MongoDB?

Advertisements