Spring 数据 MongoDb 从 String 到 ObjectId 的聚合查找

Spring data MongoDb Aggregation lookup from String to ObjectId

我正在使用 Spring(引导)数据 2.2.7 和 mongodb 4.0。 我已经设置了 3 个我试图通过聚合查找操作加入的集合。

目录

{
    "_id" : ObjectId("5ec7856eb9eb171b72f721af"),
    "model" : "HX711",
    "type" : "DIGITAL",
....
}

映射
@Document(collection = "catalog")
public class Product implements Serializable {

    @Id
    private String _id;
    @TextIndexed
    private String model;
....

库存

{
    "_id" : ObjectId("5ec78573b9eb171b72f721ba"),
    "serialNumber" : "7af646bb-a5a8-4b86-b56b-07c12a625265",
    "bareCode" : "72193.67751691974",
    "productId" : "5ec7856eb9eb171b72f721af",
......
}

映射
@Document(collection = "stock")
public class Component implements Serializable {

    @Id
    private String _id;
    private String productId;
....

productId字段指的是目录集合_id中的一个

操作

{
    "_id" : ObjectId("5ec78671b9eb171b72f721d3"),
    "componentId" : ""5ec78573b9eb171b72f721ba",
    .....

}

映射
public class Node implements Serializable {

    @Id
    private String _id;
    private String componentId;
....

componentId字段指的是股票集合中的_id

我想查询 operationsstock 集合以检索按 Product.model 排序的相应节点或组件对象列表字段(在 catalog 集合中。)

虽然目标是在 Java 中编码,但我尝试先在 Mongo shell 中提出请求,但我甚至无法让它工作,因为我正在尝试加入(查找)带有 ObjectId 的字符串:Node.componentId -> Component._id Component.productId -> Product._id

对于组件(库存)-> 产品(目录)的关系,我已经尝试过

LookupOperation lookupOperation = LookupOperation.newLookup()
        .from("catalog")
        .localField("productId")
        .foreignField("_id")
        .as("product");

TypedAggregation<Component> agg =
        Aggregation.newAggregation(
                Component.class,
                lookupOperation
        );


AggregationResults<Component> results = mongoTemplate.aggregate(agg, "stock", Component.class);
return results.getMappedResults();

但它returns整个组件记录没有产品信息。

[{"_id":"5ec78573b9eb171b72f721b0","uuId":"da8800d0-b0af-4886-80d1-c384596d2261","serialNumber":"706d93ef-abf5-4f08-9cbd-e7be0af1681c","bareCode":"90168.94737714577","productId":"5ec7856eb9eb171b72f721a9","created":"2020-05-22T07:55:31.66","updated":null}, .....]

感谢您的帮助。


注: 除了@Valijon 的回答之外,为了能够获得预期的结果,返回的对象必须包含一个 "product" 属性 或者什么都不返回(例如使用 JSON REST 服务)

public class ComponentExpanded implements Serializable {

    private String product;
....

AggregationResults<ComponentExpanded> results =
        mongoTemplate.aggregate(agg,mongoTemplate.getCollectionName(Component.class), ComponentExpanded.class);

如您所见,问题出在 productId_id 之间的类型不匹配。

要加入此类数据,我们需要执行 uncorrelated sub-queries,并不是每个 "new" 特征都会立即进入抽象层,例如 spring-mongo

试试这个:

Aggregation agg = Aggregation.newAggregation(l -> new Document("$lookup",
    new Document("from", mongoTemplate.getCollectionName(Product.class))
        .append("let", new Document("productId", new Document("$toObjectId", "$productId")))
        .append("pipeline",
                Arrays.asList(new Document("$match",
                        new Document("$expr",
                                new Document("$eq", Arrays.asList("$_id", "$$productId"))))))
        .append("as", "product")),
    Aggregation.unwind("product", Boolean.TRUE));

AggregationResults<Component> results = mongoTemplate.aggregate(agg, 
    mongoTemplate.getCollectionName(Component.class), Component.class);
return results.getMappedResults();

MongoPlayground 在这里查看 shell 查询的样子。

注意:对于Javav1.7,你需要像下面这样实现AggregationOperation

AggregationOperation l = new AggregationOperation() {

    @Override
    public Document toDocument(AggregationOperationContext context) {
        return new Document(...); // put here $lookup stage
    }

};