【问题标题】:Mongo aggregation for a DbRef field with mongoTemplate带有 mongoTemplate 的 DbRef 字段的 Mongo 聚合
【发布时间】:2020-03-17 18:51:27
【问题描述】:

假设我有两个 mongo 集合,如下所示。

Collection A
{ 
    "_id" : ObjectId("582abcd85d2dfa67f44127e0"),
    "level" : "super"
    "dataReference" : Object
        B : DbRef(B, 5b618a570550de0021aaa2ef, undefined)
}

Collection B
{ 
    "_id" : ObjectId("5b618a570550de0021aaa2ef"),  
    "role" : "admin"
}

我需要的是从 Collection A 中检索记录,这些记录的“level”字段值为“super”,其相关 Collection B 记录的“role”值为“admin”。

为此,我正在尝试使用聚合和 java mongoTemplate。

以下是我尝试过的代码,但它返回 0 条记录。

final TypedAggregation<A> typedAggregation = Aggregation.newAggregation(A.class,
                Aggregation.match(Criteria.where("level").equals(level)),
                Aggregation.lookup("B", "_id", "dataReference.B.$id", "Basb"),
                Aggregation.match(new Criteria().andOperator(
                        Criteria.where("B.role").regex("admin")
                )));

        final AggregationResults<Map> A = mongoTemplate.aggregate(typedAggregation, "A", Map.class);

请注意,我是 Mongo 聚合的新手。

【问题讨论】:

  • 您的语法错误。将Criteria.where("level").equals(level) 更改为Criteria.where("level").is(level)

标签: java mongodb aggregation-framework mongotemplate


【解决方案1】:

这是一个安静丑陋的解决方案:

蒙古模板

您不能使用TypedAggregation,因为我们需要转换A 集合才能加入B 集合

Aggregation typedAggregation = Aggregation.newAggregation(
    Aggregation.match(Criteria.where("level").is("super")),
    new AggregationOperation() {
        @Override
        public Document toDocument(AggregationOperationContext context) {
            return Document.parse("{\"$addFields\":{\"dataReference\":{\"$reduce\":{\"input\":{\"$objectToArray\":\"$dataReference\"},\"initialValue\":null,\"in\":{\"$cond\":[{\"$eq\":[{\"$type\":\"$$this.v\"},\"objectId\"]},\"$$this.v\",\"$$value\"]}}}}}");
        }
    },
    Aggregation.lookup("B", "dataReference", "_id", "B"),
    Aggregation.match(new Criteria().andOperator(
             Criteria.where("B.role").regex("admin")
        )
    )
);

final AggregationResults<Document> A = mongoTemplate.aggregate(typedAggregation, "A", Document.class);

MongoDB 聚合

db.A.aggregate([
  {
    "$match": {
      "level": "super"
    }
  },
  {
    "$addFields": {
      "B": {
        "$reduce": {
          "input": {
            "$objectToArray": "$dataReference"
          },
          "initialValue": null,
          "in": {
            "$cond": [
              {
                "$eq": [
                  {
                    "$type": "$$this.v"
                  },
                  "objectId"
                ]
              },
              "$$this.v",
              "$$value"
            ]
          }
        }
      }
    }
  },
  {
    "$lookup": {
      "from": "B",
      "localField": "B",
      "foreignField": "_id",
      "as": "B"
    }
  },
  {
    "$match": {
      "$and": [
        {
          "B.role": {
            "$regex": "admin",
            "$options": ""
          }
        }
      ]
    }
  }
])

MongoPlayground

【讨论】:

  • 如何将结果集转换为A类
  • @UdaraSeneviratne 将 Document.class 更改为 A.class
  • 实际上我得到“找不到能够从 [org.bson.types.ObjectId] 类型转换为 [com.DataReference] 类型的转换器”
  • 我不得不稍微更新一下。请参考mongoplayground.net/p/yYYlBzmAYmW
  • @UdaraSeneviratne 看看here
猜你喜欢
  • 2015-01-08
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-13
  • 2022-01-27
  • 1970-01-01
相关资源
最近更新 更多