遇到的問題-----you can't add a second 'treePath' criteria. Query already contains



當我們在java中使用mongo的spring接口進行查詢時,如果需要對同一個字段  有兩個限制條件,使用不當是則會報錯:

例如 如下用法:


Criteria.where("treePath").in(parentId).and("treePath").size(level)


treePath有兩個限制條件


報錯:

org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'treePath' expression specified as 'treePath : { "$size" : 2}'. Criteria already contains 'treePath : { "$in" : [ "50137df5f49f9b4a6481d639"]}'.




正確用法:

int parentId = 100;
int level = 5;
Criteria c = new Criteria().andOperator(Criteria.where("treePath").is(parentId),
Criteria.where("treePath").size(level));
System.out.println(c.getCriteriaObject());

輸出命令如下:

{ "$and" : [ { "treePath" : 100} , { "treePath" : { "$size" : 5}}]}




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com