
- 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 a pattern in $regex?
Yes, you can use $indexOfCP to use a filed value as a pattern. Let us create a collection with documents −
> db.demo88.insertOne( ... { ... "Name": "Chris", ... "PassoutYear": "2020", ... "websiteName": "chris.shop.com/Carol-2020-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c471bf0181ecc422b1") } > db.demo88.insertOne( ... { ... "Name": "David", ... "PassoutYear": "2010", ... "websiteName": "david.bigshop.com/David-2010-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c571bf0181ecc422b2") }
Display all documents from a collection with the help of find() method −
> db.demo88.find();
This will produce the following output −
{ "_id" : ObjectId("5e2c14c471bf0181ecc422b1"), "Name" : "Chris", "PassoutYear" : "2020", "websiteName" : "chris.shop.com/Carol-2020-" } { "_id" : ObjectId("5e2c14c571bf0181ecc422b2"), "Name" : "David", "PassoutYear" : "2010", "websiteName" : "david.bigshop.com/David-2010-" }
Following is the query to use field value as pattern in $regex −
> db.demo88.aggregate([ { "$match": { "$expr": { "$ne": [ { "$indexOfCP": ["$websiteName", "$Name"] }, -1 ] } }} ])
This will produce the following output −
{ "_id" : ObjectId("5e2c14c571bf0181ecc422b2"), "Name" : "David", "PassoutYear" : "2010", "websiteName" : "david.bigshop.com/David-2010-" }
- Related Articles
- Is it possible to use MongoDB field value as pattern in $regex?
- Is it possible to use MongoDB to query for entries that have a particular value in a field in an object in an array?
- Is it possible to use MongoDB capped collection?
- Is it possible to rename _id field after MongoDB group aggregation?
- 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 have a HTML SELECT/OPTION value as NULL using PHP?
- Is it possible to make a case-insensitive query in MongoDB?
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- Is it possible to use Python modules in Octave?
- MongoDB Regex Search on Integer Value?
- Delete a field and value in MongoDB?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to use pyplot without DISPLAY?

Advertisements