Basic Query Operators: Element Operators

Explore using MongoDB's element operators—$exists, to check if a field is present, and $type, to verify its data type—to ensure data consistency in schema-less collections.

Element operators are special query operators in MongoDB that allow us to:

  • Check if a field exists in a document.

  • Verify the data type of a field.

Note: Do you remember that MongoDB is a schema-less database? It means documents in the same collection can have different/optional fields.

Here are the two most commonly used logical operators: $exists and $type

The $exists operator

The $exists operator checks if a specified field is present in a document. The following query shows this.