MongoDB Spring - 动态获取一个值

MongoDB Spring - Get a value dynamically

我在不同的集合中有以下文档

资产

{
  "_id": "1234",
  "name": "Needle",
  "initialStock": 20
},

{
  "_id": "12345",
  "name": "Serum",
  "initialStock": 5
}

保留

{
  "_id: "12345,
  "from" : ISODate("2019-07-30T07:09:00.000Z"),
  "to" : ISODate("2019-08-30T11:00:00.000Z"),
  "assets": [
      {
     "_id": "1234",
     "assignedStock": 10
      },
     "_id": "12345",
     "assignedStock": 1
      }
    ]
}

如何在两个给定日期之间减去 assignedStockinitialStock?例如 "I want to know the available stock of this asset between two dates (the from and to)".

如何在 Spring 上完成此操作? (普通 Mongo 也会给我一个线索)。

我当前的代码如下,但我只是在 assets 处得到一个空数组:

LookupOperation lookupOperation = LookupOperation.newLookup()
       .from("assets")
       .localField("assets._id")
       .foreignField("_id")
       .as("assets");

Criteria criteria = Criteria.where("from")
       .gte(from)
       .and("to")
       .lte(to)
       .and("assets")
       .not().size(0)
       .elemMatch(
            Criteria.where("_id")
               .is(idAsset)
       );

Aggregation aggregation = Aggregation.newAggregation(match(criteria), lookupOperation);
List<Document> results =
          this.mongoTemplate.aggregate(aggregation, "reserved", Document.class).getMappedResults();

这很棘手,即使我是 MongoDB 的新手,也觉得这个查询很有挑战性。但是,我设法编写了一个查询,returns 您得到了所需的输出。我已经使用 mongo playgroud 来模拟您在问题中指定的场景。

db.reserves.aggregate([
  {
    $unwind: {
      path: "$assets"
    }
  },
  {
    $group: {
      _id: "$assets._id",
      sumAssigned: {
        $sum: "$assets.assignedStock"
      },
      
    }
  },
  {
    $lookup: {
      from: "assets",
      localField: "_id",
      foreignField: "_id",
      as: "asset_lookup"
    }
  },
  {
    $project: {
      sumAssigned: 1,
      initialStock: {
        $sum: "$asset_lookup.initialStock"
      },
      availableStock: {
        $subtract: [
          "$sumAssigned",
          {
            $sum: "$asset_lookup.initialStock"
          }
        ]
      }
    }
  }
])

https://mongoplayground.net/p/h5eMWIE5q4p // 新解

https://mongoplayground.net/p/r1bcoUjD3eG // 旧解

我使用了带展开、查找和项目运算符的聚合管道。希望对你有帮助。