Skip to content
Advertisement

Why filtering does not work with Spring Boot and MongoDB

Why filtering by import_created_at does not work?

I have a Spring Boot Application and MongoDB as database.

I have Mongo collection items and 2 documents there:

JavaScript

I have entity classes for items:

JavaScript

and

JavaScript

and

JavaScript

and

JavaScript

and

JavaScript

and

JavaScript

And I have repository class:

JavaScript

When I call the repo method findAllByFilters without filters (only by contractId = 1) it returns 2 documents as expected

When I add productId as filter and get it by contractId = 1 and productId = 11 it returns one doc as expected

BUT when I call with contractId = 1 and lastUpdate = 1661784425743 it returns nothing but should return 1st document. What is wrong here?

Advertisement

Answer

Per the comments, the code above is generating a query similar to:

JavaScript

This syntax attempts to do perform an equality match on the whole embedded document requiring an exact match of the entire filter (including field order). You can see a demonstration of that in this Mongo Playground, where the second document is the one that doesn’t match for interesting reasons.

Instead, you will want to use dot notation to query on a nested field. The syntax for that pipeline looks something like this:

JavaScript

And the associated Mongo Playground example shows both of the documents getting returned as expected.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement