
- 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
Implementing MongoDB $exists and $ne?
The $exists is used to check whethre a filed exists, whereas $ne is for not equal condition. Let us first create a collection with documents −
> db.existsDemo.insertOne({"Name":"Chris","Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3916d78f205348bc650") } > db.existsDemo.insertOne({"Name":"","Age":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c39a6d78f205348bc651") } > db.existsDemo.insertOne({"Name":null,"Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3a66d78f205348bc652") } > db.existsDemo.insertOne({"Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3c36d78f205348bc653") }
Following is the query to display all documents from a collection with the help of find() method −
> db.existsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7c3916d78f205348bc650"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5cd7c39a6d78f205348bc651"), "Name" : "", "Age" : null } { "_id" : ObjectId("5cd7c3a66d78f205348bc652"), "Name" : null, "Age" : 24 } { "_id" : ObjectId("5cd7c3c36d78f205348bc653"), "Age" : 23 }
Following is the query to implement $exists operator −
> db.existsDemo.find({"$and":[ {"Name":{"$exists":true}}, {"Name":{"$ne": ""}}]});
This will produce the following output −
{ "_id" : ObjectId("5cd7c3916d78f205348bc650"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5cd7c3a66d78f205348bc652"), "Name" : null, "Age" : 24 }
- Related Articles
- How to query MongoDB using the $ne operator?
- Implementing MongoDB map-reduce
- Implementing String Comparison in MongoDB?
- Check if MongoDB database exists?
- Check that Field Exists with MongoDB?
- Return True if a document exists in MongoDB?
- How to determine whether a field exists in MongoDB?
- How to find if element exists in document - MongoDB?
- Drop Collection if already exists in MongoDB using Python
- Consider the ambiguous grammar.\nE → E + E\nE → E * E\nE → (E)\nE → id\n(a) Construct LR (0) items for above grammar.\n(b) Construct SLR parsing table for grammar.\n(c) Parse the input string id + id * id.
- Check if value exists for a field in a MongoDB document?
- How to apply a condition only if field exists in MongoDB?
- How can I check whether a field exists or not in MongoDB?
- Find FIRST & FOLLOW for the following Grammar\nE → TE′\nE → +TE′|ε\nT → FT′\nT′ →* FT′|ε\nF → (E)|id
- Implementing Salting

Advertisements