thathashd
thathashd

Reputation: 1032

Panache MongoDB: Return only matching data from Array

Is it possible to apply a filter on the Entity in order to filter the following collection entry, based on the lang property within descriptions and subTypes.descriptions?

{
    "id": "609a85120c133e33e4cc3ed7",
    "code": "REST",
    "descriptions": [
        {
            "lang": "pt",
            "description": "Restaurante"
        },
        {
            "lang": "en",
            "description": "Restaurant"
        }
    ],
    "subTypes": [
        {
            "code": "TIP",
            "descriptions": [
                {
                    "lang": "pt",
                    "description": "Restaurante Tipico"
                },
                {
                    "lang": "en",
                    "description": "Typical Restaurante"
                }
            ]
        },
        {
            "code": "HMB",
            "descriptions": [
                {
                    "lang": "pt",
                    "description": "Restaurante Hamburger"
                },
                {
                    "lang": "en",
                    "description": "Burger"
                }
            ]
        }
    ]
}

I would like to apply the query on the list method from PanacheMongoEntity. Is this possible?

Upvotes: 0

Views: 1252

Answers (1)

loicmathieu
loicmathieu

Reputation: 5562

You can use any query possible on MongoDB on the list method of PanacheMongoEntity via a native MongoDB query (so a standard JSON query).

MongoDB allow to query an array of document, and support projection on the array element.

MongoDB with Panache projection via the find().project() method only works with field projection so you'll need to use a native MongoDB query directly on the collection, you will not be able to use the list method.

Something like this should work:

Entity.mongoCollection().find("{//your query}", "{subTypes.descriptions:{$elemMatch: {lang:'en'}}}");

Upvotes: 2

Related Questions