Spring Boot Mongo: "$elemMatch needs an Object"

Hello @Maikel_van_Dort, welcome to the community.

This error occurs when you use $elemMatch with Single Query Condition . So, if you change your query as follows, there is no error (and works as expected).

@Query("{'categories': { $regex: ?0, $options: 'i' } }")

Note that, if you need to work with an array of objects, then it becomes necessary to use the $elemMatch operator, with multiple fields of the object. For example, with a document with the categories array field with embedded documents (or objects):

{
        "_id" : 1,
        "title" : "Aardappels",
        "categories" : [
                {
                        "a" : "a1",
                        "b" : "b1"
                },
                {
                        "a" : "y1",
                        "b" : "z1"
                }
        ]
}

Then your query need to be using $emeMatch, and this runs without any errors. E.g.:

@Query("{'categories': { $elemMatch: { a: { $regex: ?0, $options: 'i' }, b: ?1 } } }")