
- 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 query to determine if a specific value does not exist?
To determine if a specific value does not exist, use $ne in MongoDB. Let us create a collection with documents −
> db.demo206.insertOne( ... { ... "ClientDetails": ... [ ... { ... "Name":"Chris", ... "Age":28, ... "CountryName":"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8bd403d395bdc21346ee") } > db.demo206.insertOne( ... { ... "ClientDetails": ... [ ... { ... "Name":"David", ... "Age":29, ... "CountryName":"UK" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8bd403d395bdc21346ef") } > > db.demo206.insertOne( ... { ... "ClientDetails": ... [ ... { ... "Name":"Bob", ... "Age":31, ... "CountryName":"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8bd503d395bdc21346f0") }
Display all documents from a collection with the help of find() method −
> db.demo206.find();
This will produce the following output −
{ "_id" : ObjectId("5e3d8bd403d395bdc21346ee"), "ClientDetails" : [ { "Name" : "Chris", "Age" : 28, "CountryName" : "US" } ] } { "_id" : ObjectId("5e3d8bd403d395bdc21346ef"), "ClientDetails" : [ { "Name" : "David", "Age" : 29, "CountryName" : "UK" } ] } { "_id" : ObjectId("5e3d8bd503d395bdc21346f0"), "ClientDetails" : [ { "Name" : "Bob", "Age" : 31, "CountryName" : "US" } ] }
Following is the query to determine if a specific value does not exist −
> db.demo206.find({"ClientDetails.CountryName": {"$ne": "US"}});
This will produce the following output −
{ "_id" : ObjectId("5e3d8bd403d395bdc21346ef"), "ClientDetails" : [ { "Name" : "David", "Age" : 29, "CountryName" : "UK" } ] }
- Related Articles
- MongoDB query for documents whose array elements does not have a specific value
- Insert records in MongoDB collection if it does not exist?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to pull a specific value from a document
- MongoDB query to increment a specific value using custom variable
- Query an array in MongoDB to fetch a specific value
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to get specific month|year (not date)?
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- How to run MongoDB query to update only a specific field value?
- MongoDB query to remove a specific document
- MySQL create user if it does not exist?
- How to select MongoDB document that does not consist a specific field?
- How to create a folder if it does not exist in C#?
- MongoDB query to group records and display a specific value with dot notation

Advertisements