【发布时间】:2019-12-07 14:05:05
【问题描述】:
我有以下文档,每个文档都在不同的集合中
资产
{
"_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
}
]
}
如何在两个给定日期之间减去 assignedStock 和 initialStock?例如“我想知道该资产在两个日期(从开始到结束)之间的可用库存”。
如何在 Spring 上做到这一点? (普通的 Mongo 也会给我一个线索)。
我当前的代码如下,但我只是在资产处得到一个空数组:
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();
【问题讨论】:
标签: spring mongodb spring-boot lookup aggregation