Home » MongoDB: How to Check if Field Exists

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 } })

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

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 here.

Additional Resources

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

MongoDB: How to List All Field Names
MongoDB: How to Rename Fields
MongoDB: How to Add New Fields
MongoDB: How to Remove Fields

You may also like