Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoRepository findByThisAndThat custom @Query with multiple parameters

I am trying to run this query

{campaignId: ObjectId('5a6b11e806f0252e1408e379'),'options.path': 'NT'}

it works perfectly in Compass, but when I try to add in MongoRepository interface like below:

@Query("{ 'campaignId': ObjectId(?0), 'options.path': ?1})")
public Dialog findByCampaignIdAndPath(String campaignId, String path);

I get this exception:

nested exception is com.mongodb.util.JSONParseException:
@Query("{ 'campaignId': ObjectId(?0), 'options.path': ?1})")

Using spring-boot version 1.5.9.RELEASE, not the buggy version

EDIT1: specified the exact id name, as suggested in comments

EDIT2: Here is my Dialog class

@Getter     
@Setter
@Document(collection = "Dialog")
public class Dialog {
    @Id
    public String id;
    private DBRef campaignId;
    private Message message;
    private List<Option> options;

    public Dialog(final Message message, final List<Option> options) {
        this.message = message;
        this.options = options;
    }
}
like image 958
shabby Avatar asked Feb 04 '23 02:02

shabby


1 Answers

Try this:

@Query("{ 'campaignId': ?0, 'options.path': ?1}")
public Dialog findByIdAndPath(String id, String path);

Or Simply:

public Dialog findByCampaignIdAndOptionsPath(String id, String path);

If Option is an embedded document

like image 183
Afridi Avatar answered Feb 07 '23 07:02

Afridi