
- 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
How can I use 'Not Like' operator in MongoDB?
For this, use the $not operator in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.notLikeOperatorDemo.insertOne({"StudentName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a29c393b406bd3df60dfc") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a29cc93b406bd3df60dfd") } > db.notLikeOperatorDemo.insertOne({"StudentName":"John Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a29df93b406bd3df60dfe") } > db.notLikeOperatorDemo.insertOne({"StudentName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a2a1693b406bd3df60dff") } > db.notLikeOperatorDemo.insertOne({"StudentName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a2a2693b406bd3df60e00") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.notLikeOperatorDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" } { "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" } { "_id" : ObjectId("5c8a29df93b406bd3df60dfe"), "StudentName" : "John Taylor" } { "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" } { "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }
Here is the query to use NOT LIKE operator in MongoDB −
> db.notLikeOperatorDemo.find( { StudentName: { $not: /^John Taylor.*/ } } );
The following is the output −
{ "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" } { "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" } { "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" } { "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" } You can use the following query also: > db.notLikeOperatorDemo.find({StudentName: {$not: /John Taylor/}}); The following is the output: { "_id" : ObjectId("5c8a29c393b406bd3df60dfc"), "StudentName" : "John Doe" } { "_id" : ObjectId("5c8a29cc93b406bd3df60dfd"), "StudentName" : "John Smith" } { "_id" : ObjectId("5c8a2a1693b406bd3df60dff"), "StudentName" : "Carol Taylor" } { "_id" : ObjectId("5c8a2a2693b406bd3df60e00"), "StudentName" : "David Miller" }
- Related Articles
- What is the use of MySQL NOT LIKE operator?
- How I can use a database-name with special characters like " customer_tracker-990" in MongoDB console
- How can I use a script to create users in MongoDB?
- How can I use $elemMatch on first level array in MongoDB?
- Can we use NOT and AND together in MongoDB?
- How can I check whether a field exists or not in MongoDB?
- What are the different wildcard characters which can be used with NOT LIKE operator?
- What is the use of SOUNDS LIKE operator in MySQL?
- What is the use of MySQL SOUNDS LIKE operator?
- How do I use the conditional operator in C/C++?
- Explain the use of sql LIKE operator using MySQL in Python?
- Use MySQL LIKE and NOT LIKE to display similar results?
- MongoDB $regex operator i or I for case insensitive search
- Can I query on a MongoDB index if my query contains the $or operator?
- How can we use MySQL UNION operator on datasets?

Advertisements