
- 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
Regex to ignore a specific character in MongoDB?
You can use regular expression along with $not operator to ignore a specific character and display rest of them. Let us first create a collection with documents −
> db.regexDemo.insertOne({"CustomerId":"Customer#1234","CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7428f8f9e6ff3eb0ce436") } > db.regexDemo.insertOne({"CustomerId":"Customer5678","CustomerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7429e8f9e6ff3eb0ce437") } > db.regexDemo.insertOne({"CustomerId":"Customer#777","CustomerName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc742ae8f9e6ff3eb0ce438") } > db.regexDemo.insertOne({"CustomerId":"Customer777","CustomerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc742bc8f9e6ff3eb0ce439") }
Following is the query to display all documents from a collection with the help of find() method −
> db.regexDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7428f8f9e6ff3eb0ce436"), "CustomerId" : "Customer#1234", "CustomerName" : "Chris" } { "_id" : ObjectId("5cc7429e8f9e6ff3eb0ce437"), "CustomerId" : "Customer5678", "CustomerName" : "Robert" } { "_id" : ObjectId("5cc742ae8f9e6ff3eb0ce438"), "CustomerId" : "Customer#777", "CustomerName" : "Carol" } { "_id" : ObjectId("5cc742bc8f9e6ff3eb0ce439"), "CustomerId" : "Customer777", "CustomerName" : "David" }
Case 1 − Here is the query to avoid using a specific character in MongoDB. The character is # −
> db.regexDemo.find({CustomerId: /^[^#]*$/}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7429e8f9e6ff3eb0ce437"), "CustomerId" : "Customer5678", "CustomerName" : "Robert" } { "_id" : ObjectId("5cc742bc8f9e6ff3eb0ce439"), "CustomerId" : "Customer777", "CustomerName" : "David" }
Case 2 − Here is another query to avoid using a specific character in MongoDB. The character is # −
> db.regexDemo.find({CustomerId: {$not: /#/}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc7429e8f9e6ff3eb0ce437"), "CustomerId" : "Customer5678", "CustomerName" : "Robert" } { "_id" : ObjectId("5cc742bc8f9e6ff3eb0ce439"), "CustomerId" : "Customer777", "CustomerName" : "David" }
- Related Articles
- Select * but ignore displaying results containing a specific character in MySQL
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- How to add space before and after specific character using regex in Python?
- Ignore null values in MongoDB documents
- How to use $regex in MongoDB?
- Java regex to exclude a specific String constant
- Set regex in MongoDB $in?
- Using regex in MongoDB findOne()
- Posix character classes Java regex
- Match specific word in regex in JavaScript?
- How to match a non-word character using Java RegEx?
- Using a regex with text search in MongoDB
- How to match any character using Java RegEx
- How to select a specific subdocument in MongoDB?
- Character class p{javaLowerCase} Java regex.

Advertisements