我有两个收藏夹: 公司:
{
"_id" : "1",
"company_code" : "A-111",
"code_description" : "BGI",
"out_company_code" : "A-111"
}
成本中心:
{
"_id" : "2,
"company_id" : "1", //Company id
"home_cost_center" : "1111",
"home_cost_center_description" : "Finance"
}
我希望查询结果如下:
{
"_id" : "2,
"company_id" : "1",
"home_cost_center" : "1111",
"home_cost_center_description" : "Finance",
"company": {
"_id" : "1",
"company_code" : "A-111",
"code_description" : "BGI",
"out_company_code" : "A-111"
}
}
为此,我创建了此查询
String queryWithCompanyCode = " { '$lookup': {" +
" 'from': 'company'," +
" 'let': { 'codeId': '$company_id' }," +
" 'pipeline': [" +
" { '$match': { '$expr': { '$eq': [{ '$toString': '$_id' }, '$$codeId'] }}}" +
" ]," +
" 'as': 'companyCode'" +
" }}," +
" {'$unwind': '$companyCode'}";
ObjectId objectId = new ObjectId(id);
AggregationOperation operation = Aggregation.match(
Criteria.where("_id").is(objectId)
);
TypedAggregation<T> typedAggregation = Aggregation.newAggregation(Output.class, operation, new CustomAggregationOperation(queryWithCompanyCode));
T result = operations.aggregate(typedAggregation, CostCenter.class, Output.class).getUniqueMappedResult();
通过上面的查询,我得到以下结果:
{
"_id" : "2,
"company_id" : null,
"home_cost_center" : null,
"home_cost_center_description" : null,
"company": {
"_id" : "1",
"company_code" : "A-111",
"code_description" : "BGI",
"out_company_code" : "A-111"
}
}
我正在使用Spring:2.1.10和mongodb:4.2.1。奇怪的是,当我在数据库中运行此查询时,它可以正常运行,但在春季无法正常运行,而且我无法弄清楚这里出了什么问题。