C#.NET – MongoDB Find Empty field in the document

C#.NET - MongoDB Find Empty field in the document

Today in the article we shall learn how to use query using C#.NET – MongoDB Find Empty Field In The Document for the given collection. We shall verify queries using MongoShell or UI tools like a Compass UI.

Updated November 2026: a table comparing empty, null and missing filters, a combined Filter.Or section, a wording fix and an FAQ.

We shall also see a C# MongoDB query to get all documents where the fields are empty in the mongo collection.

Today in this article, we will cover below aspects,

We shall also see a C# MongoDB query to get all the records fields that are not empty.

Getting started

I have a sample MongoDB document as below in one of the collections. Here we shall be trying to search all the documents where the State field is empty.

C#.NET - MongoDB Find Empty field in the document

C# Mongo- Find documents when a field is Empty

The below query matches documents where State field is empty

Query Pattern

{ Field Name : { $eq : "" } }

Example Query

{ State : {$eq : "" } }

C# Mongo Query Pattern

var tempQuery = new BsonDocument
                 {
                     {"State" , new BsonDocument {

                      { "$eq", string.Empty }
                     }}
                 };

Alternatively, please use the below builder pattern to create a query,

Find Empty field mongodb c#

Note:

In the above query $eq an aggregate query compares two values (support both value and type) and returns:

  • true when the values are matching
  • false when the values are not matching

Syntax

{ $eq: [ <expression1>, <expression2> ] }

Empty string, null and missing: which filter matches which case?

A filter for "" only matches fields stored as an empty string. Null and absent fields need different filters, so pick the one that matches the case you’re hunting.

These are the three cases, using the State field:

  • Empty string: State exists and holds "".
  • BSON null: State exists and holds an explicit null value.
  • Missing: State isn’t in the document at all.

Note: Picture a paper form with a State box. An empty string is a box someone left blank on purpose, null is a box marked “N/A”, and missing is a form printed without the box. The database treats all three as different.

This table shows which filters match each case (Yes means the document is returned):

Stored stateEq(“State”, “”)Eq(“State”, BsonNull.Value)Exists(“State”, false)Type(“State”, BsonType.Null)Ne(“State”, BsonNull.Value)
Empty stringYesNoNoNoYes
BSON nullNoYesNoYesNo
MissingNoYesYesNoNo

Why does the string.Empty query miss some documents?

A null filter matches both explicit nulls and missing fields, while an empty string matches neither. That’s why the $eq with string.Empty query earlier in this article skips documents where State is null or absent. If you want “no usable value” in every form, combine the filters with Filter.Or: one for "" and one for null, which also covers missing.

Does Exists(true) mean the field has a value?

No. Filter.Exists("State", true) still matches fields whose value is null, because the field is present. To get documents that hold a real value, use Filter.Ne("State", BsonNull.Value), which already excludes both null and missing. Add a second Ne for "" with Filter.And if empty strings should go too.

What are the common traps?

Builders<T>.Filter.Empty is not an empty-field filter. It’s an empty filter definition that matches every document in the collection. Use it as a starting point when you build filters in a loop, not to find blank fields.

Older snippets sometimes use $type: 0 to find missing fields. Since MongoDB 4.2 that no longer stands in for $exists: false, so use Filter.Exists("State", false) instead.

C# Mongo- Find documents when a field is not Empty

Query Pattern

{ Field Name : { $ne: "" } }

Example Query

{ State : {$ne: "" } }

C# Mongo Query

The below query matches documents where the “State” field is empty or “”

var tempQuery = new BsonDocument
                 {
                     {"State" , new BsonDocument {

                      { "$ne", string.Empty }
                     }}
                 };


C# mongo driver field is not Empty document mongoose

That’s all, So today we learned the simple mongo query for identifying the records where the field is an empty string (a null or missing field needs a different filter). It’s pretty easy to prepare the query for such needs.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published (2026) best practices and guidelines for software design and development.



Frequently asked questions

Does { field: null } match missing fields in MongoDB?

Yes. A null equality filter matches documents where the field is explicitly null and documents where the field doesn’t exist. It does not match an empty string, because "" is a different value. To match only explicit nulls, use Filter.Type with BsonType.Null. To match only missing fields, use Filter.Exists(field, false).

Does $exists: true return documents where the field is null?

Yes. A field set to null still exists, so Filter.Exists(field, true) matches it. To get only fields with a real value, use Filter.Ne(field, BsonNull.Value), or combine Exists with a not-null check. On an unindexed field, $exists: true scans the whole collection, so keep that in mind on large data sets.

How do I find documents where a field is not empty in C# MongoDB?

Use Filter.Ne(field, string.Empty) for non-empty strings. That filter alone still returns documents where the field is null or missing, since neither equals an empty string. Add Filter.Ne(field, BsonNull.Value) inside Filter.And if you also want to exclude those. Whitespace-only strings need a regex filter, because they aren’t equal to "".

How do I check for an empty array field in the MongoDB C# driver?

Use Filter.Size(field, 0) to match arrays with zero elements. It won’t match null or missing fields. If you need those too, combine it with a null check using Filter.Or.

Leave a Reply

Your email address will not be published. Required fields are marked *