遇到的问题-----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}}]}



你可能感兴趣的:(java,spring,mongo,query,already,contai)