
- 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 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" }
- Related Articles
- MongoDB query to skip documents
- MongoDB query to get distinct FirstName values from documents
- Finding documents in MongoDB collection where a field is equal to given integer value?
- Alternate method to find DBA from given IP address
- Transition from IPv4 to IPv6 address
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Query for documents where array size is greater than 1 in MongoDB?
- MySQL query to copy IP address from varchar column to integer in the same table?
- How to fire find query on sub-documents in MongoDB?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query to skip n first documents?
- How to find the latitude and longitude from address in Android?
- MongoDB - Query embedded documents?

Advertisements