Board logo

标题: you can't add a second 'treePath' criteria. Query already contains [打印本页]

作者: look_w    时间: 2019-5-11 11:35     标题: 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}}]}




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0