
- 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 to query MongoDB using the $ne operator?
To query MongoDB using the $ne operator, following is the syntax −
db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty();
Let us create a collection with documents −
> db.notEqaulToDemo.insertOne({"StudentName":"Larry","StudentMathMarks":68}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded") } > db.notEqaulToDemo.insertOne({"StudentName":"Chris","StudentMathMarks":88}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd3a79de8cc557214c0dee") } > db.notEqaulToDemo.insertOne({"StudentName":"David","StudentMathMarks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd3a89de8cc557214c0def") } > db.notEqaulToDemo.insertOne({"StudentName":"Carol","StudentMathMarks":69}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd3a97de8cc557214c0df0") }
Display all documents from a collection with the help of find() method −
> db.notEqaulToDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"), "StudentName" : "Larry", "StudentMathMarks" : 68 } { "_id" : ObjectId("5cbd3a79de8cc557214c0dee"), "StudentName" : "Chris", "StudentMathMarks" : 88 } { "_id" : ObjectId("5cbd3a89de8cc557214c0def"), "StudentName" : "David", "StudentMathMarks" : 45 } { "_id" : ObjectId("5cbd3a97de8cc557214c0df0"), "StudentName" : "Carol", "StudentMathMarks" : 69 }
Following is the query for MongoDB $ne operator −
> db.notEqaulToDemo.find({StudentMathMarks:{$ne:88}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"), "StudentName" : "Larry", "StudentMathMarks" : 68 } { "_id" : ObjectId("5cbd3a89de8cc557214c0def"), "StudentName" : "David", "StudentMathMarks" : 45 } { "_id" : ObjectId("5cbd3a97de8cc557214c0df0"), "StudentName" : "Carol", "StudentMathMarks" : 69 }
- Related Articles
- How to query with nand operator in MongoDB?
- MongoDB query to implement OR operator in find()
- Implementing MongoDB $exists and $ne?
- Can I query on a MongoDB index if my query contains the $or operator?
- How to implement MongoDB $or operator
- Using positional operator with hierarchies in MongoDB?
- MongoDB query to update an array using FindAndUpdate()?
- How to query MongoDB with “like”?
- How to implement MongoDB $or query?
- How to dynamically build MongoDB query?
- How to query MongoDB similar to “like” ?
- Using $addFields pipeline and run with the MongoDB $filter operator
- MongoDB query to group several fields using aggregation framework?
- How to query all items in MongoDB?
- How to query MongoDB with a LIMIT?

Advertisements