MongoDB - match records based on array of objects property value

 

MongoDB - match records based on array of objects property value


I have to write a query for finding out documents based on array of objects property values. For example, I have following documents.


[{
    status: 'active',
    disabled: false,
    contents: [
    {
        label: 'Name',
        slug: 'name',
        value: 'ABC',
    },
    {
        label: 'Slug',
        slug: 'slug',
        value: 'abc',
    },
    {
        label: 'Featured Product',
        slug: 'featured-product',
        value: 'Yes',
    },
    {
        label: 'In Stock',
        slug: 'in-stock',
        value: 'Yes',
    }]
},
{
    status: 'active',
    disabled: false,
    contents: [
        {
            label: 'Name',
            slug: 'name',
            value: 'XYZ',
        },
        {
            label: 'Slug',
            slug: 'slug',
            value: 'xyz',
        },
        {
            label: 'Featured Product',
            slug: 'featured-product',
            value: 'No',
        },
        {
            label: 'In Stock',
            slug: 'in-stock',
            value: 'Yes',
        }
    ]
}]

I have to write a query for finding out documents based on array of objects property values. For example, I have following documents -

[{
    status: 'active',
    disabled: false,
    contents: [
    {
        label: 'Name',
        slug: 'name',
        value: 'ABC',
    },
    {
        label: 'Slug',
        slug: 'slug',
        value: 'abc',
    },
    {
        label: 'Featured Product',
        slug: 'featured-product',
        value: 'Yes',
    },
    {
        label: 'In Stock',
        slug: 'in-stock',
        value: 'Yes',
    }]
},
{
    status: 'active',
    disabled: false,
    contents: [
        {
            label: 'Name',
            slug: 'name',
            value: 'XYZ',
        },
        {
            label: 'Slug',
            slug: 'slug',
            value: 'xyz',
        },
        {
            label: 'Featured Product',
            slug: 'featured-product',
            value: 'No',
        },
        {
            label: 'In Stock',
            slug: 'in-stock',
            value: 'Yes',
        }
    ]
}]

Now, I have to find out documents where slug is featured-productvalue is Yes & slug is in-stockvalue is Yes.

I wrote a query to find out documents where slug is featured-product but having difficulties when applying another condition - slug is in-stockvalue is Yes.

Here is my query that is working fine.

db.items.aggregate([
{
    $lookup: {
        from: "contents",
        localField: "_id",
        foreignField: "itemId",
        as: "contents"
    }
},
{
    $match: {
        contents: {
            $elemMatch: {
                "slug": "featured-product",
                "value": "No"
            },
        }
    }
}])

I have to write a query for finding out documents based on array of objects property values. For example, I have following documents -

[{
    status: 'active',
    disabled: false,
    contents: [
    {
        label: 'Name',
        slug: 'name',
        value: 'ABC',
    },
    {
        label: 'Slug',
        slug: 'slug',
        value: 'abc',
    },
    {
        label: 'Featured Product',
        slug: 'featured-product',
        value: 'Yes',
    },
    {
        label: 'In Stock',
        slug: 'in-stock',
        value: 'Yes',
    }]
},
{
    status: 'active',
    disabled: false,
    contents: [
        {
            label: 'Name',
            slug: 'name',
            value: 'XYZ',
        },
        {
            label: 'Slug',
            slug: 'slug',
            value: 'xyz',
        },
        {
            label: 'Featured Product',
            slug: 'featured-product',
            value: 'No',
        },
        {
            label: 'In Stock',
            slug: 'in-stock',
            value: 'Yes',
        }
    ]
}]

Now, I have to find out documents where slug is featured-productvalue is Yes & slug is in-stockvalue is Yes.

I wrote a query to find out documents where slug is featured-product but having difficulties when applying another condition - slug is in-stockvalue is Yes.

Here is my query that is working fine -

db.items.aggregate([
{
    $lookup: {
        from: "contents",
        localField: "_id",
        foreignField: "itemId",
        as: "contents"
    }
},
{
    $match: {
        contents: {
            $elemMatch: {
                "slug": "featured-product",
                "value": "No"
            },
        }
    }
}])

Thanks in advance.

Edit:

Here is the result of items collection after lookup. I am adding some parts of the whole result.


[
    {
        _id: ObjectId("648d6d0f221f77c425aac3b9"),
        status: 'active',
        disabled: false,
        createdBy: ObjectId("64898858d1c108f6eec71217"),
        createdAt: 2023-06 - 17T08: 21: 35.355Z,
        updatedAt: 2023-06 - 17T08: 21: 35.355Z,
        contents: [
            {
                _id: ObjectId("648d6d0f221f77c425aac3cf"),
                label: 'Name',
                itemId: ObjectId("648d6d0f221f77c425aac3b9"),
                slug: 'name',
                value: 'V-NECK DRESS',
                trashed: 'false',
                status: 1,
                widgetId: ObjectId("648d4111ae1b9fb2af57f8f7"),
                createdAt: 2023-06 - 17T08: 21: 35.374Z,
                updatedAt: 2023-06 - 17T10:09:02.733Z,
                disabled: 'false'
            },
            {
                ....
            }
        ]
    },
    {
        ......
    }
]


Learn more about this post please follow the link

Post a Comment

0 Comments