
- 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 - How to check for equality in collection and in embedded document?
For this, check using $where in MongoDB. Let us create a collection with documents −
> db.demo292.insertOne({FirstName:"Chris",LastName:"Brown", ... "Friend":{FirstName:"David","LastName":"Miller"} ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10aa5d93261e4bc9ea30") } > db.demo292.insertOne({FirstName:"John",LastName:"Doe", ... "Friend":{FirstName:"Mike","LastName":"Doe"} ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10dc5d93261e4bc9ea31") }
Display all documents from a collection with the help of find() method −
> db.demo292.find();
This will produce the following output −
{ "_id" : ObjectId("5e4c10aa5d93261e4bc9ea30"), "FirstName" : "Chris", "LastName" : "Brown", "Friend" : { "FirstName" : "David", "LastName" : "Miller" } } { "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", "LastName" : "Doe", "Friend" : { "FirstName" : "Mike", "LastName" : "Doe" } }
Following is the query to check for equality in collection and in embedded document −
> db.demo292.find({$where: 'this.Friend.LastName === this.LastName'})
This will produce the following output −
{ "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", "LastName" : "Doe", "Friend" : { "FirstName" : "Mike", "LastName" : "Doe" } }
- Related Articles
- MongoDB query for fields in embedded document?
- How to get embedded data in a MongoDB document?
- Implement MongoDB $push in embedded document array?
- Check for Existing Document in MongoDB?
- Return specific MongoDB embedded document
- Check for existing documents/embedded documents in MongoDB
- How to find a certain element in the MongoDB embedded document?
- MongoDB query to return only embedded document?
- Increment a field in MongoDB document which is embedded?
- Add a field to an embedded document in an array in MongoDB?
- How to update an existing document in MongoDB collection using Java?
- In MongoDB how do you use $set to update a nested value/embedded document?
- How to delete document from a collection in MongoDB using deleteOne() method?
- Retrieving the first document in a MongoDB collection?
- How to check empty field in a MongoDB collection?

Advertisements