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

伊曼纽尔·布鲁内特

我在mongodb 4.0中使用Spring(boot)数据2.2.7。我已经设置了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一个股票集合

我想查询操作库存集合以检索Product.model字段(在目录集合中)排序的相应Node或Component对象列表

虽然目标是使用Java编写代码,但我尝试首先在Mongo shell中发出请求,但由于尝试将对象ID与字符串连接在一起(查找),所以我什至无法使它正常工作:Node.componentId- > Component._id Component.productId-> Product._id

对于关系Component(stock)-> Product(Catalog),我尝试了

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();

但是它将返回没有产品信息的整个组件记录。

[{"_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

要加入此类数据,我们需要执行不相关的子查询,并且并非每个“新”功能都会使其立即进入抽象层,例如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在这里检查外壳查询的外观。

注意:对于Java v1.7,您需要实现AggregationOperation如下所示:

AggregationOperation l = new AggregationOperation() {

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

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 ObjectId 的 mongodb 聚合查找

来自分类Dev

MongoDB聚合查询的Spring数据

来自分类Dev

MongoDb聚合查询转换为Spring数据

来自分类Dev

MongoDB-在ObjectId上聚合$ match

来自分类Dev

MongoDB聚合以实际文档替换ObjectId数组

来自分类Dev

Mongodb聚合查找2级别,创建了结果的新条目,而不是仅填充具有objectId的列

来自分类Dev

Spring数据等效于MongoDB中的以下聚合操作

来自分类Dev

MongoDB Spring数据,复杂条件下的最大聚合

来自分类Dev

Spring数据MongoDB聚合中的属性始终为空

来自分类Dev

MongoDB聚合查询,查找失败

来自分类Dev

我在mongodb聚合中使用查找,但是它不返回我要查找的数据

来自分类Dev

MongoDB聚合-分组数据

来自分类Dev

使用Spring-Data-MongoDB的1.3.5-RELEASE,Spring数据MongoDB聚合函数不起作用

来自分类Dev

MongoDB聚合项目字符串转换为ObjectId

来自分类Dev

Spring Data MongoDB聚合转换?

来自分类Dev

Spring Data MongoDB聚合方案

来自分类Dev

使用 spring 的 MongoDB 聚合查询

来自分类Dev

MongoDB聚合包含伪数据

来自分类Dev

具有查找功能的mongodb聚合

来自分类Dev

MongoDB使用聚合管道查找和更新

来自分类Dev

MongoDB在嵌套数组聚合中查找

来自分类Dev

MongoDB聚合项目查找中的特定字段

来自分类Dev

MongoDB聚合双重查找和管道

来自分类Dev

MongoDB:聚合查询中的Near +查找

来自分类Dev

Spring数据存储库聚合

来自分类Dev

spring 数据 Mongo db 聚合

来自分类Dev

mongodb在保存数据时生成相同的ObjectID

来自分类Dev

如何在mongodb中查找文档并使用聚合查找属性?

来自分类Dev

MongoDB的Spring数据-gt,gte,lt,lte运算符的聚合错误

Related 相关文章

热门标签

归档