
- 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 Regex Search on Integer Value?
To perform Regex search on integer value, you need to use $where operator. The syntax is as follows:
db.yourCollectionName.find({ $where: "/^yourIntegerPatternValue.*/.test(this.yourFieldName)" });
To understand the above concept, let us create a collection with document. The query to create a collection with document is as follows:
> db.regExpOnIntegerDemo.insertOne({"StudentId":2341234}); { "acknowledged" : true, "insertedId" : ObjectId("5c70370c75eb1743ddddce21") } > db.regExpOnIntegerDemo.insertOne({"StudentId":123234}); { "acknowledged" : true, "insertedId" : ObjectId("5c70371175eb1743ddddce22") } > db.regExpOnIntegerDemo.insertOne({"StudentId":9871234}); { "acknowledged" : true, "insertedId" : ObjectId("5c70371875eb1743ddddce23") } > db.regExpOnIntegerDemo.insertOne({"StudentId":2345612}); { "acknowledged" : true, "insertedId" : ObjectId("5c70372275eb1743ddddce24") } > db.regExpOnIntegerDemo.insertOne({"StudentId":1239812345}); { "acknowledged" : true, "insertedId" : ObjectId("5c70372975eb1743ddddce25") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.regExpOnIntegerDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c70370c75eb1743ddddce21"), "StudentId" : 2341234 } { "_id" : ObjectId("5c70371175eb1743ddddce22"), "StudentId" : 123234 } { "_id" : ObjectId("5c70371875eb1743ddddce23"), "StudentId" : 9871234 } { "_id" : ObjectId("5c70372275eb1743ddddce24"), "StudentId" : 2345612 } { "_id" : ObjectId("5c70372975eb1743ddddce25"), "StudentId" : 1239812345 }
Here is the query to do Regex search on integer value:
> db.regExpOnIntegerDemo.find({ $where: "/^123.*/.test(this.StudentId)" });
The following is the output:
{ "_id" : ObjectId("5c70371175eb1743ddddce22"), "StudentId" : 123234 } { "_id" : ObjectId("5c70372975eb1743ddddce25"), "StudentId" : 1239812345 }
- Related Articles
- Using a regex with text search in MongoDB
- MongoDB $regex operator i or I for case insensitive search
- How to search documents through an integer array in MongoDB?
- Search a sub-field on MongoDB?
- Perform nested document value search in MongoDB?
- MongoDB find() to operate on recursive search?
- Significance of regex match() and regex search() function in Python
- How to search for documents based on the value of adding two properties in MongoDB?
- Is it possible to use MongoDB field value as pattern in $regex?
- Using regex in MongoDB findOne()
- Set regex in MongoDB $in?
- Is it possible to use MongoDB field value as a pattern in $regex?
- Search a value in an object with number keys with MongoDB
- How to use $regex in MongoDB?
- Avoid MongoDB performance issues while using regex

Advertisements