CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Mongo DB > How to check if field exists in MongoDb?

How to check if field exists in MongoDb?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: November 30, 2021 ยท 1 min read ยท 0 Comments
Share: X in ๐Ÿ”—
How to check if field exists in MongoDb?

In MongoDB, we can check whether the field exists or not in two different ways:

  • By using the $exists operator and
  • By setting the value of that field to null

Ignore field using $exists operator

Let’s say we have a collection called users and we want to query documents where the age field is exist or not. The query would look like this:

db.users.find({
    age: { $exists: false } 
})

This query returns all the user’s documents where the age field is missing.

Ignore field setting field value to null

We can also verify whether the particular field exists in the MongoDB document or not by using the following query:

db.users.find({
    age: null 
})

What the above query does is:

The { age : null } query matches documents that either contain the age field whose value is null or that do not contain the age field.

Either way, we can check whether the field in the MongoDB document exists or not.

Reference: https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields/

Related Posts:

  • Array Operator in MongoDB
  • Comparison Operator in MongoDB
  • find() command in MongoDB
  • MongoDB bulkWrite in Java: A Comprehensive Guide
  • What is MongoDB?
  • How to Write User Stories and Acceptance Criteria: A…
Tags:mongodb
Was this article helpful?
โ† Previous ArticleDifference Between Authentication and Authorization
Next Article โ†’The Internet and Java’s Place in IT

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap