【发布时间】:2022-01-09 12:38:04
【问题描述】:
我想使用 Mongodb 聚合匹配操作通过 String 中多个嵌套对象的对象 ID 过滤 mongodb 集合。但是spring data mongodb不会在匹配操作中将String值转换为对象Id。
我能够通过字符串值中的多个文档 ID(主键,而不是嵌套对象的对象 ID)过滤文档,而没有任何问题,因为 Spring 数据 mongodb 将字符串值转换为 oid:
{ "_id" : { "$in" : [{ "$oid" : "61a31853d268434139e7fc11"}, { "$oid" : "61a31853d268434139e7fc12"}]}
我想要达到的目标如下:
db.getCollection('products').aggregate(
[
{ "$match" : { "$and" : [{ "type._id" : { "$in" : [
ObjectId("618b99a3b4c24465b074b246"),
ObjectId("60afc0920dab8b6d3ac26355")
] }}]}}
])
但我总是得到以下信息:
db.getCollection('products').aggregate(
[
{ "$match" : { "$and" : [{ "type._id" : { "$in" : [
[{ "$oid" : "618b99a3b4c24465b074b246"}, { "$oid" : "60afc0920dab8b6d3ac26355"}]
]}}]}}
])
Spring data mongodb 为 $in json 中的 OID 生成二维数组
我的 Mongodb 实体:
@Document(collection = "products")
public class Product {
@Id
private String id;
@NotNull
private ProductType type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Type getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
}
@Document(collection = "product_types")
public class ProductType {
@Id
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
我执行聚合的java代码:
List<String> typeIds = Arrays.asList("618b99a3b4c24465b074b246", "60ad10ffc723877d8a977149");
List<AggregationOperation> aggregateOperations = new ArrayList<>();
Criteria criteria = Criteria.where("type._id").in(typeIds.stream().map(t -> new ObjectId(t)).collect(Collectors.toList()));
aggregateOperations.add(Aggregation.match(criteria));
Aggregation aggregation = Aggregation.newAggregation(aggregateOperations);
mongoTemplate.aggregate(aggregation, "products", ProductListDTO.class);
mongodb采集数据如下:
{
"_id" : ObjectId("61a31853d268434139e7fc11"),
"type" : {
"_id" : ObjectId("618b99a3b4c24465b074b246")
}
}
【问题讨论】:
标签: mongodb spring-boot aggregation-framework spring-data-mongodb