I have the following JSON structure:
{
"communication": {
"office": {
"email": "[email protected]"
},
"private": {
"email": "[email protected]"
},
}
}
I want to query dynamically for the email based on the type e.g. office or private. When I use the following command:
@Query(value = "{ 'communication.?0.email' : ?1 }")
Object findByEmail(String type, String email);
The
'communication.?0.email'
is converted to
'communication."office".email'
and mongo didn't find an entry. How can I avoid the quotes before and after office?
Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes.
Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.
Simple answer is spring mongo doesn't supports what you are looking for. Why you are not passing everything as parameter rather as below.
@Query(value = "{ 'communication.?0.email' : ?1 }") Object findByEmail(String type, String email);
where email value should
type= "communication." + type + ".email"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With