Skip to main content
Version: Next

Element query operators

Element query operators return data based on the existence of a specified field or the data type of a particular value.

OperatorDescription
$existsreturns documents where a field exists or does not exist
$typematches document containing elements with the specified type

For the examples in this section, insert the following documents into the electronics collection:

db.electronics.insertMany([
{
product: 'laptop',
price: 1500,
stock: 5,
discount: true,
specifications: [
{
processor: 'Intel Core i7'
},
{
memory: 16
}
]
},
{
product: 'phone',
price: 800,
stock: 10,
discount: true,
specifications: [
{
brand: 'Apple'
},
{
model: 'iPhone 12'
}
]
},
{
product: 'tablet',
price: 500,
stock: 15,
discount: true,
specifications: [
{
brand: 'Samsung'
},
{
model: 'Galaxy Tab S7'
}
]
},
{
product: 'keyboard',
price: 100,
stock: 20
},
{
product: 'mouse',
price: 50,
stock: 25,
discount: null,
specifications: []
},
{
product: 'monitor',
price: 250,
stock: 30,
discount: true,
specifications: [
{
size: 27
},
{
resolution: '4K'
}
]
},
{
product: 'printer',
price: 150,
stock: 35,
discount: false
},
{
product: 'scanner',
price: 100,
stock: 40,
discount: true,
specifications: [
{
type: 'flatbed'
}
]
}
])

$exists

Syntax: { <field>: { $exists: <boolean> } }

To find out if a particular field exists in a document, use the $exists operator.

tip

If the <boolean> value is true, the query returns documents where the specified field exists, even if the value is null or an empty array. If the <boolean> value is false, the query returns documents where the specified field does not exist.

Example: Find documents in the electronics collection where the specifications field exists using the $exists operator:

db.electronics.find({
specifications: {
$exists: true
}
})

The output:

[
{
_id: ObjectId("63a32fc7cf72d6203bb45b8e"),
product: 'laptop',
price: 1500,
stock: 5,
discount: true,
specifications: [{ processor: 'Intel Core i7' }, { memory: 16 }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b8f"),
product: 'phone',
price: 800,
stock: 10,
discount: true,
specifications: [{ brand: 'Apple' }, { model: 'iPhone 12' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b90"),
product: 'tablet',
price: 500,
stock: 15,
discount: true,
specifications: [{ brand: 'Samsung' }, { model: 'Galaxy Tab S7' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b92"),
product: 'mouse',
price: 50,
stock: 25,
discount: null,
specifications: []
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b93"),
product: 'monitor',
price: 250,
stock: 30,
discount: true,
specifications: [{ size: 27 }, { resolution: '4K' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b95"),
product: 'scanner',
price: 100,
stock: 40,
discount: true,
specifications: [{ type: 'flatbed' }]
}
]

In the above output, the query returns all documents where the specifications field exists, even when the field has an empty value.

If you want to find documents where the specifications field exists and has a specific value, use the $exists operator in conjunction with other operators.

Example: The following query returns all documents where the specifications field exists and its value is an array:

db.electronics.find({
specifications: {
$exists: true,
$type: 'array'
}
})

The output:

[
{
_id: ObjectId("63a32fc7cf72d6203bb45b8e"),
product: 'laptop',
price: 1500,
stock: 5,
discount: true,
specifications: [{ processor: 'Intel Core i7' }, { memory: 16 }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b8f"),
product: 'phone',
price: 800,
stock: 10,
discount: true,
specifications: [{ brand: 'Apple' }, { model: 'iPhone 12' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b90"),
product: 'tablet',
price: 500,
stock: 15,
discount: true,
specifications: [{ brand: 'Samsung' }, { model: 'Galaxy Tab S7' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b92"),
product: 'mouse',
price: 50,
stock: 25,
discount: null,
specifications: []
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b93"),
product: 'monitor',
price: 250,
stock: 30,
discount: true,
specifications: [{ size: 27 }, { resolution: '4K' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b95"),
product: 'scanner',
price: 100,
stock: 40,
discount: true,
specifications: [{ type: 'flatbed' }]
}
]

$type

Syntax: { <field>: { $type: <datatype> } }

Use the $type operator to select documents where the data type of a field matches the specified BSON type

The <datatype> parameter can be the type code or alias of the particular data type.

The following table lists the available BSON type codes and their corresponding aliases:

Type codeTypeAlias
1Doubledouble
2Stringstring
3Objectobject
4Arrayarray
5Binary databinData
7ObjectIdobjectId
8Booleanbool
9Datedate
10Nullnull
11Regular expressionregex
1632-bit integerint
17Timestamptimestamp
1864-bit integerlong
19Decimal128decimal
-1Min keyminKey
127Max keymaxKey
-128Numbernumber
caution

Decimal128, Min Key, and Max Key are not currently implemented. FerretDB supports the alias number which matches the following BSON types: Double, 32-bit integer, and 64-bit integer type values.

info

FerretDB supports the alias number which matches the following BSON types: Double, 32-bit integer, and 64-bit integer type values.

Example: The following operation query returns all documents in the electronics collection where the discount field has a boolean data type, which can be represented with the data code 8:

db.electronics.find({
discount: {
$type: 8
}
})

This query can also be written using the alias of the specified data type.

db.electronics.find({
discount: {
$type: 'bool'
}
})

The output:

[
{
_id: ObjectId("63a32fc7cf72d6203bb45b8e"),
product: 'laptop',
price: 1500,
stock: 5,
discount: true,
specifications: [{ processor: 'Intel Core i7' }, { memory: 16 }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b8f"),
product: 'phone',
price: 800,
stock: 10,
discount: true,
specifications: [{ brand: 'Apple' }, { model: 'iPhone 12' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b90"),
product: 'tablet',
price: 500,
stock: 15,
discount: true,
specifications: [{ brand: 'Samsung' }, { model: 'Galaxy Tab S7' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b93"),
product: 'monitor',
price: 250,
stock: 30,
discount: true,
specifications: [{ size: 27 }, { resolution: '4K' }]
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b94"),
product: 'printer',
price: 150,
stock: 35,
discount: false
},
{
_id: ObjectId("63a32fc7cf72d6203bb45b95"),
product: 'scanner',
price: 100,
stock: 40,
discount: true,
specifications: [{ type: 'flatbed' }]
}
]