Java 创建类似“查询”的查询;A是“A”;b";C是''&引用;(带Mongo的弹簧引导/弹簧数据)
我正在使用MongoDB和Spring数据为MongoDB制作一个简单的Spring启动应用程序(2.3.4)。我通常使用@Query注释为应用程序创建查询,效果非常好。但对于我想要使用的聚合,我使用Criteria类构建了一个查询。我需要的标准是Java 创建类似“查询”的查询;A是“A”;b";C是''&引用;(带Mongo的弹簧引导/弹簧数据),java,spring,mongodb,spring-boot,spring-data-mongodb,Java,Spring,Mongodb,Spring Boot,Spring Data Mongodb,我正在使用MongoDB和Spring数据为MongoDB制作一个简单的Spring启动应用程序(2.3.4)。我通常使用@Query注释为应用程序创建查询,效果非常好。但对于我想要使用的聚合,我使用Criteria类构建了一个查询。我需要的标准是 其中(“主”)是(值),而(“次”)是(“”) 我需要所有的条目,其中primary等于'value',secondary为空。在MOngoDB Compass中输入的查询 {$and:[{primary:'value'},{secondary:'}
其中(“主”)是(值),而(“次”)是(“”
)
我需要所有的条目,其中primary等于'value',secondary为空。在MOngoDB Compass中输入的查询
{$and:[{primary:'value'},{secondary:'}]}
正如预期的那样工作,但是当我尝试使用Spring的标准时,看起来与secondary的和部分被完全删除了。我得到的任何结果在初级阶段都是“有价值的”,而在次级阶段则是“有价值的”。这意味着一个空字段或其他任何内容。将.is(“”
部分替换为.regex(“^$”)
没有帮助
对我来说这看起来很基本,那么我还缺什么呢?我不想用“空标志”替换空的辅助设备,因为这感觉不对
更新:
这就是问题中的代码
Criteria crit;
if(!primary.equals(secondary)) {
crit = where("primary").is(primary.name()).and("secondary").is(secondary.name());
} else {
crit = where("primary").is(primary.name()).and("secondary").is("");
}
MatchOperation matchStage = Aggregation.match(crit);
GroupOperation groupStage = Aggregation.group("grouping").count().as("sum");
SortOperation sortStage = new SortOperation(Sort.by("_id"));
Aggregation aggregation = Aggregation.newAggregation(matchStage, groupStage, sortStage);
AggregationResults<TypePerGroup> results = mongoTemplate.aggregate(aggregation, "dataCollection", TypePerGroup.class);
标准crit;
如果(!primary.equals(secondary)){
crit=其中(“primary”)是(primary.name()),而(“secondary”)是(secondary.name());
}否则{
crit=其中(“primary”)是(primary.name()),而(“secondary”)是(“”);
}
MatchOperation matchStage=聚合.match(crit);
GroupOperation groupStage=Aggregation.group(“分组”).count().as(“总和”);
SortOperation sortStage=新的排序操作(排序依据(“\u id”);
聚合聚合=聚合。新聚合(matchStage、groupStage、sortStage);
AggregationResults=mongoTemplate.aggregate(聚合,“数据收集”,TypePerGroup.class);
这适用于mongodb-不确定compass添加了什么。两个查询不会生成相同的json查询,但它们是相等的
生成的查询
where("primary").is(value).and("secondary").is("").
是
也许指南针不喜欢这种变体
无论如何,要生成类似于您在compass中输入的内容的查询,您可以使用下面的代码
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("primary").is("hello"), Criteria.where("secondary").is(""));
Query query = Query.query(criteria);
你没有遗漏任何东西
其中(“primary”).is(value).和(“secondary”).is(“”)是正确的,并且在功能上等同于{$and:[{primary:'value'},{secondary:'}]}
。您应该为MongoTemplate打开调试级日志记录以查看生成的查询。A已使用Mongo DBCompas连接到Atlas,并向集合中添加了4条记录:
[{
"primary": "A",
"secondary": "A"
},{
"primary": "A",
"secondary": ""
},{
"primary": "B",
"secondary": "B"
},{
"primary": "B",
"secondary": ""
}]
两个问题:
List<Data> firstResults = mongoTemplate.query(Data.class)
.matching(Query.query(Criteria.where("primary").is("B").and("secondary").is("")))
.all();
System.out.println(firstResults);
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("primary").is("B"), Criteria.where("secondary").is(""));
List<Data> secondResults = mongoTemplate.query(Data.class)
.matching(Query.query(criteria))
.all();
System.out.println(secondResults);
Campfire您能提供您的代码示例以进行分析吗?是的,对于一个简单的查询,它确实工作得很好。但是,一旦添加了聚合的其余部分,结果就与预期的数字不匹配。我按照您的要求添加了代码段。您能提供返回错误结果的数据吗?请发布一个显示聚合中使用的字段的示例输入文档。
List<Data> firstResults = mongoTemplate.query(Data.class)
.matching(Query.query(Criteria.where("primary").is("B").and("secondary").is("")))
.all();
System.out.println(firstResults);
Criteria criteria = new Criteria();
criteria.andOperator(Criteria.where("primary").is("B"), Criteria.where("secondary").is(""));
List<Data> secondResults = mongoTemplate.query(Data.class)
.matching(Query.query(criteria))
.all();
System.out.println(secondResults);
[Data{primary='B', secondary=''}]