
- 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
Is it possible to use MongoDB field value as pattern in $regex?
Yes, for this, use $indexOfCP operator along with aggregate framework. Let us first create a collection with documents −
> db.patterDemo.insertOne( { "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68d") } > db.patterDemo.insertOne( { "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.patterDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" } { "_id" : ObjectId("5cea40acef71edecf6a1f68e"), "ClientName" : "Carol", "ClientWebsiteName" : "pqrs.com" }
Following is the query to use field value as pattern in $regex −
> db.patterDemo.aggregate([ { "$match": { "$expr": { "$ne": [ { "$indexOfCP": ["$ClientWebsiteName", "$ClientName"] }, -1 ] } }} ]);
This will produce the following output −
{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" }
- Related Articles
- Is it possible to use MongoDB field value as a pattern in $regex?
- Is it possible to use MongoDB capped collection?
- Is it possible to rename _id field after MongoDB group aggregation?
- Is it possible to use MongoDB to query for entries that have a particular value in a field in an object in an array?
- How to use $regex in MongoDB?
- Is it possible to cast in a MongoDB Query?
- Is it possible to achieve a slice chain in MongoDB?
- Is it possible to use Python modules in Octave?
- MongoDB Regex Search on Integer Value?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to use pyplot without DISPLAY?
- Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?
- Is it possible to make a case-insensitive query in MongoDB?
- Particular field as result in MongoDB?
- Is it possible to exclude nested fields in MongoDB with a wildcard?

Advertisements