Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 query to find “where” Billing Address is Equal to Delivery Address from documents?
To check equality and fetch the documents, use $where in MongoDB. Let us create a collection with documents −
> db.demo589.insertOne({deliveryAddress:"US",billingAddress:"UK"});{
"acknowledged" : true, "insertedId" : ObjectId("5e92c117fd2d90c177b5bccc")
}
> db.demo589.insertOne({deliveryAddress:"US",billingAddress:"US"});{
"acknowledged" : true, "insertedId" : ObjectId("5e92c11bfd2d90c177b5bccd")
}
> db.demo589.insertOne({deliveryAddress:"US",billingAddress:"AUS"});{
"acknowledged" : true, "insertedId" : ObjectId("5e92c11ffd2d90c177b5bcce")
}
> db.demo589.insertOne({deliveryAddress:"UK",billingAddress:"US"});{
"acknowledged" : true, "insertedId" : ObjectId("5e92c127fd2d90c177b5bccf")
}
Display all documents from a collection with the help of find() method −
> db.demo589.find();
This will produce the following output −
{ "_id" : ObjectId("5e92c117fd2d90c177b5bccc"), "deliveryAddress" : "US", "billingAddress" : "UK" }
{ "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : "US" }
{ "_id" : ObjectId("5e92c11ffd2d90c177b5bcce"), "deliveryAddress" : "US", "billingAddress" : "AUS" }
{ "_id" : ObjectId("5e92c127fd2d90c177b5bccf"), "deliveryAddress" : "UK", "billingAddress" : "US" }
Here is the query to check “where” Billing Address is Equal to Delivery Address and fetch the documents −
> db.demo589.find( { $where: "this.deliveryAddress == this.billingAddress" } );
This will produce the following output −
{ "_id" : ObjectId("5e92c11bfd2d90c177b5bccd"), "deliveryAddress" : "US", "billingAddress" : "US" }Advertisements