How to find specific array elements in MongoDB document with query and filter with range?

To find specific array elements in MongoDB documents with query and filter with range, use the aggregation pipeline with $match to filter documents and $filter to filter array elements based on price range conditions.

Syntax

db.collection.aggregate([
    { $match: { field: "value" } },
    { 
        $addFields: {
            arrayField: {
                $filter: {
                    input: "$arrayField",
                    cond: {
                        $and: [
                            { $gt: ["$$this.field", minValue] },
                            { $lt: ["$$this.field", maxValue] }
                        ]
                    }
                }
            }
        }
    }
]);

Sample Data

db.demo351.insertMany([
    {
        "_id": "101",
        "ProductDetails": [
            {
                "ProductName": "Product-1",
                "ProductPrice": 500
            },
            {
                "ProductName": "Product-2",
                "ProductPrice": 400
            }
        ]
    },
    {
        "_id": "102",
        "ProductDetails": [
            {
                "ProductName": "Product-3",
                "ProductPrice": 200
            },
            {
                "ProductName": "Product-4",
                "ProductPrice": 800
            }
        ]
    }
]);
{
    "acknowledged": true,
    "insertedIds": {
        "0": "101",
        "1": "102"
    }
}

Display All Documents

db.demo351.find();
{
    "_id": "101", "ProductDetails": [
        { "ProductName": "Product-1", "ProductPrice": 500 },
        { "ProductName": "Product-2", "ProductPrice": 400 }
    ]
}
{
    "_id": "102", "ProductDetails": [
        { "ProductName": "Product-3", "ProductPrice": 200 },
        { "ProductName": "Product-4", "ProductPrice": 800 }
    ]
}

Example: Filter Products by Price Range

Find products in document "102" with price between 600 and 900 ?

db.demo351.aggregate([
    {
        $match: { _id: "102" }
    },
    {
        $addFields: {
            ProductDetails: {
                $filter: {
                    input: "$ProductDetails",
                    cond: {
                        $and: [
                            { $gt: ["$$this.ProductPrice", 600] },
                            { $lt: ["$$this.ProductPrice", 900] }
                        ]
                    }
                }
            }
        }
    }
]);
{ "_id": "102", "ProductDetails": [{ "ProductName": "Product-4", "ProductPrice": 800 }] }

How It Works

  • $match filters documents by specific criteria (document ID "102")
  • $filter processes the ProductDetails array
  • $$this.ProductPrice references each array element's ProductPrice field
  • $and ensures both price conditions are met (greater than 600 AND less than 900)

Conclusion

Use MongoDB aggregation with $match and $filter to find specific array elements within documents based on range conditions. The $filter operator processes array elements and returns only those matching the specified criteria.

Updated on: 2026-03-15T02:35:12+05:30

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements