Spring Data MongoDB to find a query that does not return results

I have a mongodb collection with several documents of the following structure

{
    "_id" : "...",
    "_class" : "...",
    ...
    "travelers" : [
        {
            "id": "12345",
            "type": "XYZ"
        },
        {
            "id": "67890",
            "type": "ABC"
        }],
    ...
}

Using spring data, I can get documents that have the XYZ traveler type:

Query query = new Query(Criteria.where("travelers.type").is("XYZ");
List<Something> something = mongoTemplate.find(query, Something.class, COLLECTION_NAME);

But if I switch the select query with the traveler ID, I get no results:

Query query = new Query(Criteria.where("travelers.id").is("12345");
List<Something> something = mongoTemplate.find(query, Something.class, COLLECTION_NAME);

I turned on all the logs, and the outgoing request {"travelers.id" : "12345"}looks correct. Running a query {"travelers.id" : "12345"}directly on db returns the correct results.

The other queries I tried in the collection work fine. I tried both with the index and without it on travel.id and no results in both cases. What am I doing wrong?

+3
source share
1 answer

"id" - . http://docs.spring.io/spring-data/data-mongodb/docs/current/reference/html/#mongo-template.id-handling

, , spring id "_id" , :

Query query = new Query(Criteria.where("travelers.id").is("12345");

, mongo,

{"travelers.id" : "12345"}

{"travelers._id" : "12345"}

, spring mongo , , mongo.

, spring .

id travelerId, .

+4

All Articles