How to Check if Field Exists in MongoDB

To check if a field exists in a MongoDB collection, you can use the $exists operator in a query document. This operator returns true if the specified field exists in the document and false if it does not. You can also use the $type operator to check the type of a field, which can be useful if you’re expecting a field of a certain type but it doesn’t exist.


You can use the following methods to check if a field exists in a collection in MongoDB:

Method 1: Check if Field Exists

db.myCollection.find({ "myField": { $exists: true } })

This method checks if “myField” exists in the collection titled myCollection. If it does, it returns all documents that contain the field name. If it doesn’t, it returns nothing.

Method 2: Check if Embedded Field Exists

db.myCollection.find({ "myField.embeddedField": { $exists: true } })

This method checks if the field name “embeddedField” within the field “myField” exists in the collection titled myCollection. If it does, it returns all documents that contain the field name. If it doesn’t, it returns nothing.

The following examples show how to use each method in practice with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", class: {conf:"Western", div:"A"}, points: 31})
db.teams.insertOne({team: "Spurs", class: {conf:"Western", div:"A"}, points: 22})
db.teams.insertOne({team: "Jazz", class: {conf:"Western", div:"B"}, points: 19})
db.teams.insertOne({team: "Celtics", class: {conf:"Eastern", div:"C"}, points: 26})

Example 1: Check if Field Exists

The following code shows how to check if the field name “points” exists in the teams collection:

db.teams.find({ "points": { $exists: true } })

This query returns the following documents:

{ _id: ObjectId("6203d10c1e95a9885e1e7637"),
  team: 'Mavs',
  class: { conf: 'Western', div: 'A' },
  points: 31 }
{ _id: ObjectId("6203d10c1e95a9885e1e7638"),
  team: 'Spurs',
  class: { conf: 'Western', div: 'A' },
  points: 22 }
{ _id: ObjectId("6203d10c1e95a9885e1e7639"),
  team: 'Jazz',
  class: { conf: 'Western', div: 'B' },
  points: 19 }
{ _id: ObjectId("6203d10c1e95a9885e1e763a"),
  team: 'Celtics',
  class: { conf: 'Eastern', div: 'C' },
  points: 26 } 

Since the field name “points” exists, every document that contains the “points” field is returned.

Suppose we instead check if the field name “steals” exists in the teams collection:

db.teams.find({ "steals": { $exists: true } })

Example 2: Check if Embedded Field Exists

The following code shows how to check if the embedded field name “div” exists within the field “class” in the teams collection:

db.teams.find({ "class.div": { $exists: true } })

This query returns the following documents:

{ _id: ObjectId("6203d10c1e95a9885e1e7637"),
  team: 'Mavs',
  class: { conf: 'Western', div: 'A' },
  points: 31 }
{ _id: ObjectId("6203d10c1e95a9885e1e7638"),
  team: 'Spurs',
  class: { conf: 'Western', div: 'A' },
  points: 22 }
{ _id: ObjectId("6203d10c1e95a9885e1e7639"),
  team: 'Jazz',
  class: { conf: 'Western', div: 'B' },
  points: 19 }
{ _id: ObjectId("6203d10c1e95a9885e1e763a"),
  team: 'Celtics',
  class: { conf: 'Eastern', div: 'C' },
  points: 26 } 

Since the embedded field name “div” exists in the “class” field, every document that contains the “div” embedded field is returned.

Suppose we instead check if the embedded field name “division” exists within the field “class” in the teams collection:

db.teams.find({ "class.division": { $exists: true } })

Since this embedded field doesn’t exist, no output is returned.

Note: You can find the complete documentation for the $exists function .

The following tutorials explain how to perform other common operations in MongoDB:

x