
- 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, finding all documents where property id equals to record id?
For this, use $where and compare with ==. Let us create a collection with documents −
> db.demo69.insertOne( ... { ... "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), ... "Details" : { ... ... "id" : ObjectId("507c7f79bcf86cd7994f6c0e") ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0e") } > db.demo69.insertOne( ... { ... "_id" : ObjectId("507c7f79bcf86cd7994f6c0f"), "Details" : { ... "id" :ObjectId("507c7f79bcf86cd7994f6c0a") ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0f") }
Display all documents from a collection with the help of find() method −
> db.demo69.find();
This will produce the following output −
{ "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), "Details" : { "id" : ObjectId("507c7f79bcf86cd7994f6c0e") } } { "_id" : ObjectId("507c7f79bcf86cd7994f6c0f"), "Details" : { "id" : ObjectId("507c7f79bcf86cd7994f6c0a") } }
Following is the query to find all documents where property id equals to record id −
> db.demo69.find({ ... $where: function(){ ... return this._id.toString() == this.Details.id.toString(); ... } ... }).pretty();
This will produce the following output −
{ "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), "Details" : { "id" : ObjectId("507c7f79bcf86cd7994f6c0e") } }
- Related Articles
- Can I retrieve multiple documents from MongoDB by id?
- MongoDB query to display all the values excluding the id?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- How to prevent MongoDB from returning the object ID while finding a document?
- HTML DOM id Property
- Match ID and fetch documents with $eq in MongoDB in case of array?
- How to print id of record in Android sqlite?
- MySQL query to find all rows where ID is divisible by 4?
- Hide id field in MongoDB
- How to find by id in MongoDB?
- MySQL query to delete a record with the lowest ID?
- Is there an operator in MySQL to implement multiple NOT conditions like WHERE id != 5 AND id != 10 AND id != 15?
- Matching MongoDB collection items by id?
- Finding documents in MongoDB collection where a field is equal to given integer value?
- JavaScript - Merge two arrays according to id property

Advertisements