MongoDB Element Query Operators and Array Query Operators

MongoDB Query Operators

1. Element Query Operators

Element operators are used to check the existence and data type of a field.

$exists

Checks whether a field exists in a document.

db.emp.find({
    "empno": { $exists: true }
})

Example:

db.emp.find({
    "empno": { $exists: true, $in: [2, 5] }
})

This finds documents where:

  • empno exists
  • empno is either 2 or 5

So, if employees 2 and 5 exist, those records will be displayed.


$nin

$nin means “not in”.

db.emp.find({
    "empno": { $nin: [20, 15] }
})

This displays documents where empno is not 20 and not 15.

Note: $nin can also match documents where the field does not exist.


$type

$type is used to find documents based on the BSON data type of a field.

Correct syntax:

db.emp.find({
    "empno": { $type: "string" }
})

This finds employees whose empno is stored as a string.

For example:

{ empno: "101" }

will match, while:

{ empno: 101 }

will not match.


2. $type Examples

Consider this collection:

db.s1.insertMany([
    { "_id": 1, address: "2030 Martian Way", zipCode: "90698345" },
    { "_id": 2, address: "156 Lunar Place", zipCode: 43339374 },
    { "_id": 3, address: "2324 Pluto Place", zipCode: NumberLong(3921412) },
    { "_id": 4, address: "55 Saturn Ring", zipCode: NumberInt(88602117) },
    { "_id": 5, address: "104 Venus Drive", zipCode: ["834847278", "1893289032"] }
])

Find strings

db.s1.find({
    "zipCode": { $type: "string" }
})

This matches document 1 because:

zipCode: "90698345"

is a string.

You can also use BSON type number:

db.s1.find({
    "zipCode": { $type: 2 }
})

2 represents string.


Find numbers

db.s1.find({
    "zipCode": { $type: "number" }
})

This matches numeric values such as:

  • 43339374
  • NumberLong(3921412)
  • NumberInt(88602117)

You can also use:

db.s1.find({
    "zipCode": { $type: 1 }
})

1 represents double, so this is not equivalent to "number".

Important correction

Your note has:

db.s1.find({ "zipCode": { $type: 1 } })

and comments it as number.

That’s not quite correct.

$typeMeaning
1double
2string
16int
18long
"number"Any numeric BSON type

So for all numeric types, prefer:

db.s1.find({
    zipCode: { $type: "number" }
})

3. Multiple $type Values

You can check multiple data types using an array.

db.s1.find({
    "zipCode": { $type: [2, 1] }
})

or:

db.s1.find({
    "zipCode": { $type: ["string", "double"] }
})

This means:

Find documents where zipCode is either a string OR a double.

Important correction

Your example uses:

"zipcode"

but your field is:

"zipCode"

MongoDB field names are case-sensitive, so use:

db.s1.find({
    "zipCode": { $type: ["string", "double"] }
})

4. Array Query Operators

Array operators are used when a field contains an array.

Let’s create the database:

use skills

Create collection:

db.createCollection("sdata")

Insert records:

db.sdata.insertMany([
    {
        Name: "Balaji",
        skills: ["Dancing", "Cooking", "Singing"]
    },
    {
        Name: "Ramesh",
        skills: ["Cooking", "Singing"]
    },
    {
        Name: "Suresh",
        skills: ["Dancing", "Singing"]
    }
])

5. $all

$all finds documents where an array contains all specified values.

db.sdata.find({
    skills: { $all: ["Cooking", "Dancing"] }
})

Result

Balaji

Why?

Balaji has:

Dancing
Cooking
Singing

Both Cooking AND Dancing are present.

Ramesh:

Cooking
Singing

❌ Dancing is missing.

Suresh:

Dancing
Singing

❌ Cooking is missing.

Easy definition

$all = Array must contain ALL specified values.


6. $size

$size finds documents where an array has an exact number of elements.

db.sdata.find({
    skills: { $size: 2 }
})

Result

Ramesh
Suresh

Because both have exactly 2 skills.

Ramesh:

["Cooking", "Singing"]

→ 2 elements ✅

Suresh:

["Dancing", "Singing"]

→ 2 elements ✅

Balaji:

["Dancing", "Cooking", "Singing"]

→ 3 elements ❌


Quick Revision

OperatorPurposeExample
$existsChecks field existence{age: {$exists:true}}
$inMatches any value from list{empno: {$in:[2,5]}}
$ninDoes not match listed values{empno: {$nin:[20,15]}}
$typeChecks BSON data type{empno: {$type:"string"}}
$allArray contains all values{skills: {$all:["Cooking","Dancing"]}}
$sizeExact array length{skills: {$size:2}}

Simple way to remember

Element Query Operators

  • $exists → Does field exist?
  • $type → What type is the field?
  • $in → Is value in this list?
  • $nin → Is value NOT in this list?

Array Query Operators

  • $all → Does array contain all these values?
  • $size → How many elements are in the array?

By admin

Leave a Reply

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