MongoDB 聚合 - 如何使用 spring-data-mongodb 将查询表达式应用到匹配阶段?
MongoDB Aggregation - How can i apply query expression into match stage using spring-data-mongodb?
我有包含动态字段的文档,我需要为给定的复杂查询条件查找匹配记录的计数
示例实体
@Document(collection = "UserAttributes")
public class UserAttributesEntity {
@Id
@Getter
private String id;
@NotNull
@Size(min = 1)
@Getter @Setter
private String userId;
@NotNull
@Getter @Setter
private Map<String, Object> attributes = new HashMap<>();
}
示例数据
{
"_id" : ObjectId("6164542362affb14f3f2fef6"),
"userId" : "89ee6942-289a-48c9-b0bb-210ea7c06a88",
"attributes" : {
"age" : 61,
"name" : "Name1"
}
},
{
"_id" : ObjectId("6164548045cc4456792d5325"),
"userId" : "538abb29-c09d-422e-97c1-df702dfb5930",
"attributes" : {
"age" : 40,
"name" : "Name2",
"location" : "IN"
}
}
预期的查询表达式
"((attributes.name == 'Name1' && attributes.age > 40) OR (attributes.location == 'IN'))
$match 的 MongoDB 聚合查询如下所示,但是无法通过 spring mongo db api:
{
$expr:
{
"$and": [{
"$gt": ["$attributes.age", 40]
}, {
"$eq": ["$attributes.name", "Name2"]
}]
}
}
我在这里遗漏了什么吗?
库使用:org.springframework.data:spring-data-mongodb:3.1.1
您可以实施自己的 AggregationOperation
来处理您的各种情况。 没试过我自己的代码,但应该是这样的:
AggregationOperation myMatch (List<Document> conditions) {
return new AggregationOperation() {
@Override
public String getOperator() {
return "$match";
}
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$match",
new Document("$expr",
new Document("$and", conditions)
)
);
}
};
}
并这样称呼它(以匹配您的问题查询):
void callMyMatch() {
myMatch(List.of(
new Document("$gt", List.of("$attributes.age", 40)),
new Document("$eq", List.of("$attributes.name", "Name2"))
));
}
项目阶段允许我们使用查询表达式,我将我的方法转换为以下方法以实现结果:
private Mono<Long> aggregate() {
final Aggregation aggregation = Aggregation
.newAggregation(
Aggregation.project("userAttributes.playerLevel", "userAttributes.name")
.andExpression("((userAttributes.name == 'Name1' && userAttributes.age > 40) OR (userAttributes.location == 'IN'))")
.as("result"),
Aggregation.match(Criteria.where("result").is(true)),
Aggregation.group().count().as("count"));
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(UserAttributesEntity.class), Map.class)
.map(result -> Long.valueOf(result.get("count").toString()))
.next()
}
我有包含动态字段的文档,我需要为给定的复杂查询条件查找匹配记录的计数
示例实体
@Document(collection = "UserAttributes")
public class UserAttributesEntity {
@Id
@Getter
private String id;
@NotNull
@Size(min = 1)
@Getter @Setter
private String userId;
@NotNull
@Getter @Setter
private Map<String, Object> attributes = new HashMap<>();
}
示例数据
{
"_id" : ObjectId("6164542362affb14f3f2fef6"),
"userId" : "89ee6942-289a-48c9-b0bb-210ea7c06a88",
"attributes" : {
"age" : 61,
"name" : "Name1"
}
},
{
"_id" : ObjectId("6164548045cc4456792d5325"),
"userId" : "538abb29-c09d-422e-97c1-df702dfb5930",
"attributes" : {
"age" : 40,
"name" : "Name2",
"location" : "IN"
}
}
预期的查询表达式
"((attributes.name == 'Name1' && attributes.age > 40) OR (attributes.location == 'IN'))
$match 的 MongoDB 聚合查询如下所示,但是无法通过 spring mongo db api:
{
$expr:
{
"$and": [{
"$gt": ["$attributes.age", 40]
}, {
"$eq": ["$attributes.name", "Name2"]
}]
}
}
我在这里遗漏了什么吗?
库使用:org.springframework.data:spring-data-mongodb:3.1.1
您可以实施自己的 AggregationOperation
来处理您的各种情况。 没试过我自己的代码,但应该是这样的:
AggregationOperation myMatch (List<Document> conditions) {
return new AggregationOperation() {
@Override
public String getOperator() {
return "$match";
}
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$match",
new Document("$expr",
new Document("$and", conditions)
)
);
}
};
}
并这样称呼它(以匹配您的问题查询):
void callMyMatch() {
myMatch(List.of(
new Document("$gt", List.of("$attributes.age", 40)),
new Document("$eq", List.of("$attributes.name", "Name2"))
));
}
项目阶段允许我们使用查询表达式,我将我的方法转换为以下方法以实现结果:
private Mono<Long> aggregate() {
final Aggregation aggregation = Aggregation
.newAggregation(
Aggregation.project("userAttributes.playerLevel", "userAttributes.name")
.andExpression("((userAttributes.name == 'Name1' && userAttributes.age > 40) OR (userAttributes.location == 'IN'))")
.as("result"),
Aggregation.match(Criteria.where("result").is(true)),
Aggregation.group().count().as("count"));
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(UserAttributesEntity.class), Map.class)
.map(result -> Long.valueOf(result.get("count").toString()))
.next()
}