Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$elemMatch equivalent in spring data mongodb

I need to know the equivalent code in spring data mongo db to the code below:-

db.inventory.find( {
                     qty: { $all: [
                                    { "$elemMatch" : { size: "M", num: { $gt: 50} } },
                                    { "$elemMatch" : { num : 100, color: "green" } }
                                  ] }
                   } )
like image 431
Vaibhav Avatar asked Mar 05 '15 09:03

Vaibhav


People also ask

How to query MongoDB with spring data?

Documents Query One of the more common ways to query MongoDB with Spring Data is by making use of the Query and Criteria classes – which very closely mirror native operators. 2.1. Is This is simply a criterion using equality – let's see how it works.

How do I use elemmatch query in MongoDB?

The elemmatch query operator allows us to match documents with an array field that contains at least one element that matches the specified query criteria. As a rule of thumb, MongoDB doesn’t allow you to use the $where operator and the $text operator with the elemmatch query operator.

What is @query annotation in MongoDB?

If we can't represent a query with the help of a method name, or criteria, we can do something more low level – use the @Query annotation. With this annotation, we can specify a raw query – as a Mongo JSON query string. 4.1.

What is a regex query in MongoDB?

A more flexible and powerful type of query is the regex. This c reates a criterion using a MongoDB $regex that returns all records suitable for this regexp for this field. It works similar to startingWith, endingWith operations – let's look at an example. We're now looking for all users that have names starting with A.


2 Answers

I am able to get the answer. This can be done in Spring data mongodb using following code

Query query = new Query();      
query.addCriteria(Criteria.where("qty").elemMatch(Criteria.where("size").is("M").and("num").gt(50).elemMatch(Criteria.where("num").is(100).and("color").is("green"))));
like image 164
Vaibhav Avatar answered Oct 22 '22 09:10

Vaibhav


Just to make @Vaibhav answer a bit more clearer.

given Document in DB

{
   "modified": true,
   "items": [
     {
      "modified": true,
      "created": false
     },
     {
      "modified": false,
      "created": false
     },
     {
      "modified": true,
      "created": true
     }
   ]
}

You could do following Query if you need items where both attribute of an item are true.

 Query query = new Query();
    query.addCriteria(Criteria.where("modified").is(true));
    query.addCriteria(Criteria.where("items")
            .elemMatch(Criteria.where("modified").is(true)
                    .and("created").is(true)));

here an example how to query with OR in elemMatch

 Query query = new Query();
        query.addCriteria(Criteria.where("modified").is(true));
        query.addCriteria(Criteria.where("items")
                .elemMatch(new Criteria().orOperator(
                        Criteria.where("modified").is(true), 
                        Criteria.where("created").is(true))));
like image 34
DCO Avatar answered Oct 22 '22 11:10

DCO