Find Records Where Array Field is Not Empty in MongoDB

It is common to use arrays as field values in MongoDB documents to store different types of data. Let’s say we have a collection of colleges where each document represents a different college and each contains an array having different courses the college offers. Now suppose we have to perform some kind of analysis operation on the collection, in that case, it is important to remove unnecessary data including the array that does not contain any values. Here comes the saver, $ne operator.

In this tutorial, I will try to explain everything about the $ne operator and we work with it to find records where the array field is not empty, not null and not undefined.

An Introduction to $ne Operator

To find records where the array field is not empty, we can use the $ne operator. The $ne operator in MongoDB stands for “not equal”. It is a type of comparison operator used to query for documents where the value of a specified field does not match the value passed with it.

Syntax:

To specifically find the records where the array is not empty, we have to pass the query filter to the find() method having an array field with the query condition containing the $ne operator along with an empty array. 

db.collection.find({ arrayField: { $ne: []} })

Parameters:

  • collection is the name of the collection in which you want to query for the documents,
  • arrayField is the name of the array field you want to check for not being empty

Return:

The above syntax selects documents from a specified collection where the arrayField is not an empty array.

Find Records Where Array Field Is Not Empty

Mongo Shell is the fastest and quickest way of interacting with the MongoDB database. Let’s see an example to demonstrate the use of the $ne operator in Mongo Shell.

First, we need some kind of database to work with. I recommend you create a database, create a collection and insert some data for demonstration.

If you want to know how to do this, here are all the links:

Done? Now open the terminal and switch to the database:

show dbs
use drones

Use the find() method to list all records that exist inside a collection in the console:

{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3ae"),
        "onSale" : true,
        "name" : "Caminara Frucha FCZ-89",
        "price" : 67800,
        "weight" : "35 kilograms",
        "additionalDetails" : {
                "material" : "glass fibre",
                "moreUses" : [ ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3af"),
        "onSale" : true,
        "name" : "Azehsra Chelske AZC-HYU56",
        "price" : 45600,
        "weight" : "32 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture"
                ]
        },
        "utility" : null
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3b0"),
        "onSale" : true,
        "name" : "Hazela Gunce QY999",
        "price" : 33900,
        "weight" : "20 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Videography",
                        "Wildlife Watching",
                        "Photography"
                ]
        },
        "utility" : [
                undefined
        ]
}

Following is the query to select records where the nested array is not empty using the $ne operator:

> db.drones.find({ "additionalDetails.moreUses": { $ne: [] } }).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3af"),
        "onSale" : true,
        "name" : "Azehsra Chelske AZC-HYU56",
        "price" : 45600,
        "weight" : "32 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture"
                ]
        },
        "utility" : null
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3b0"),
        "onSale" : true,
        "name" : "Hazela Gunce QY999",
        "price" : 33900,
        "weight" : "20 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Videography",
                        "Wildlife Watching",
                        "Photography"
                ]
        },
        "utility" : [
                undefined
        ]
}

As a result, we have found all those records where the embedded array field is not empty.

Find Records Where Array Fields Is Not Null

To find the MongoDB documents where array fields are not null, you have to pass the query filter to the find() method having an array field with the query condition containing the $ne operator along with the value null. 

Following is the query to select records where the nested array is not null:

> db.drones.find({ "utility": { $ne: null } }).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3ae"),
        "onSale" : true,
        "name" : "Caminara Frucha FCZ-89",
        "price" : 67800,
        "weight" : "35 kilograms",
        "additionalDetails" : {
                "material" : "glass fibre",
                "moreUses" : [ ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}

Find MongoDB Records Where Array Field Is Not Undefined

For Finding MongoDB records where array fields are not undefined, you can use the $ne operator with an array containing undefined.

Following is the query to select records where the nested array is not undefined:

> db.drones.find({ "utility": { $ne: [ undefined ] } }).pretty()
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0c"),
        "onSale" : false,
        "name" : "Nimbari Gryphon Medeta 65",
        "price" : 77500,
        "weight" : "77 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0d"),
        "onSale" : false,
        "name" : "X-Strimmer Eye",
        "price" : 23500,
        "weight" : "24 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Precision Agriculture",
                        "Cinematography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0e"),
        "onSale" : false,
        "name" : "Khai Balemosh Shefqa TRX",
        "price" : 120500,
        "weight" : "80 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf0f"),
        "onSale" : false,
        "name" : "Sifinist Croma AX",
        "price" : 99500,
        "weight" : "97 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("61673f46b34f185eb7b2bf10"),
        "onSale" : false,
        "name" : "Drovce Finnifield FR-7",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ac"),
        "onSale" : true,
        "name" : "PlayGarra 2078-56",
        "price" : 15000,
        "weight" : "5 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ad"),
        "onSale" : true,
        "name" : "Frinty Gemini 3X70",
        "price" : 55000,
        "weight" : "22 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("6195514a50f8bacf51bdb6ae"),
        "onSale" : true,
        "name" : "Rilche Gabbana Flier RG950",
        "price" : 110000,
        "weight" : "100 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture",
                        "Land Inspection",
                        "Water Inspection",
                        "Videography"
                ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3ae"),
        "onSale" : true,
        "name" : "Caminara Frucha FCZ-89",
        "price" : 67800,
        "weight" : "35 kilograms",
        "additionalDetails" : {
                "material" : "glass fibre",
                "moreUses" : [ ]
        },
        "utility" : [
                "Recreation",
                "Photography",
                "Videography",
                "Delivery",
                "Natural Resource Exploration"
        ]
}
{
        "_id" : ObjectId("619924ef4b1ff00cfd5eb3af"),
        "onSale" : true,
        "name" : "Azehsra Chelske AZC-HYU56",
        "price" : 45600,
        "weight" : "32 kilograms",
        "additionalDetails" : {
                "material" : "carbon fibre",
                "moreUses" : [
                        "Cinematography",
                        "Wildlife Watching",
                        "Precision Agriculture"
                ]
        },
        "utility" : null
}

In all the above queries, we have used find() with pretty(), if you want to know what it is, check out the tutorial: Pretty Print in MongoDB: What it is & How to Use it?

Conclusion

In this article, we have found the solution to the question “How to find the records where the field having array is not empty”, along with finding the records where the array field is not null, and not defined. We have used the operator named $ne for all of these operations. Hope you find the answer you’re looking for. 

Reference

Find MongoDB records where array field is not empty – Stack Overflow

Aneesha S
Aneesha S
Articles: 172