Spring Mongo Query by Example on Sub document

Viewed 664

I am trying to do a query by example on a sub document in MongoDB using Spring Data Mongo.

Assuming the object in the database looks something like this:

{
   _id : ObjectId("..."),
   child : {
      attribute1 : "value1"
   }
}

I would like to pass an example of the child to then filter the collection by that. I tried this:

Criteria.where("child").elemMatch(Criteria.byExample(sampleObject))

This seems to generate the correct query for MongoDB, but it seems that $elemMatch only works on array fields.

Then I tried:

Criteria.where("child").alike(Example.of(sampleObject))

That results in an exception:

java.lang.RuntimeException: json can't serialize type : class org.springframework.data.domain.example
0 Answers
Related