user1982978
user1982978

Reputation: 61

Spring data mongo db find in map of values

I have the following DB structure:

{  id : "1233454",
   name : "abc",
   elements : [ 
        { "1" : { id : "123", referenceId : "567" } },
        { "2" : { id : "345" } },
   ],
},
{  id : "56789",
   name : "def",
   elements : [ 
        { "3" : { id : "123", referenceId : "789" } },
        { "4" : { id : "345" } },
   ],
},
{  id : "98765",
   name : "def",
   elements : [ 
        { "3" : { id : "123", referenceId : "789" } },
        { "4" : { id : "345" } },
   ],
}

and I need to retrieve document id where referenceId is equals to the parameter passed to the method. For example: If method receives "789" it should return "56789" and "98765".

Thanks in advance.

Upvotes: 0

Views: 3521

Answers (1)

chk
chk

Reputation: 534

It is not recommended to have unknown keys if you want to query efficiently, see for example this discussion. If your keys in the elements (e.g. "1") follow a certain structure or are limited, you could use a simple $or in the query to check all options such as "elements.1.referenceId" - see here how to do this.

Upvotes: 1

Related Questions