
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 - 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 Questions & Answers
- MongoDB query for fields in embedded document?
- Return specific MongoDB embedded document
- How to get embedded data in a MongoDB document?
- Check for Existing Document in MongoDB?
- Check for existing documents/embedded documents in MongoDB
- Implement MongoDB $push in embedded document array?
- MongoDB query to return only embedded document?
- How to find a certain element in the MongoDB embedded document?
- Increment a field in MongoDB document which is embedded?
- Add a field to an embedded document in an array in MongoDB?
- Check two numbers for equality in Java
- Check two HashMap for equality in Java
- Check two ArrayList for equality in Java
- Filter query on array of embedded document with MongoDB?
- How to update an existing document in MongoDB collection using Java?
Advertisements