Sebastian
Sebastian

Reputation: 776

Extra quotes being added to @Query

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?

Upvotes: 7

Views: 988

Answers (2)

Gex
Gex

Reputation: 2202

Using SpEL should suit your needs:

@Query(value = "?#{ { 'communication.' + [0] + '.email' : ?1 } }")

The main changes are bolded out: @Query(value = "?#{ { 'communication.' + [0] + '.email' : ?1 } }")

This way, you can get rid of the extra quotes.

Upvotes: 1

sanjay patel
sanjay patel

Reputation: 449

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"

Upvotes: 1

Related Questions