$elemMatch equivalent in spring data mongodb

筅森魡賤 提交于 2020-01-11 05:30:42

问题


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" } }
                                  ] }
                   } )

回答1:


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"))));



回答2:


I think query in your answer generated below query

{ "qty" : { "$elemMatch" : { "num" : 100 , "color" : "green"}}}

I think thats not you need.

Its only check last elemMatch expression not all.

Try with this.

query = new Query();
Criteria first = Criteria.where("qty").elemMatch(Criteria.where("size").is("M").and("num").gt(50));
Criteria two = Criteria.where("qty").elemMatch(Criteria.where("num").is(100).and("color").is("green"));
query.addCriteria(new Criteria().andOperator(first, two));


来源:https://stackoverflow.com/questions/28874941/elemmatch-equivalent-in-spring-data-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!