Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

Help with $and and .elemMatch() in spring data mongoDB

$
0
0
Here is the MongoDB query I am trying to reproduce:
Code:

db.myCollection.find({
    $and: [
        {'key.attributes': {$elemMatch: {'name': 'k1', 'value': 'kv1'}}},
        {'key.attributes': {$elemMatch: {'name': 'k2', 'value': 'kv2'}}}
    ]
})

I have verified that this query produces the correct output using the mongo shell. I try to query it using spring data like this:

Code:

where("key.attributes").elemMatch(where("name").is("k1").and("value").is("kv1"))
.andOperator(
where("key.attributes").elemMatch(where("name").is("k2").and("value").is("kv2"))
);

However, this produces the following query:

Code:

{ "key.attributes" : { "$elemMatch" : { "name" : "k1" , "value" : "kv1"}} , "$and" : [ { "key.attributes" : { "$elemMatch" : { "name" : "k2" , "value" : "kv2"}}}]}
How can I get spring data to produce the query as shown above?

Viewing all articles
Browse latest Browse all 128

Trending Articles