当localField的类型为ObjectId,而ForeignField的类型为String时,如何使用spring数据引用两个mongodb集合?
ProjectionOperation convertId=Aggregation.project().and("_id").as("agentId");
LookupOperation activityOperation = LookupOperation.newLookup().
from("activity").
localField("agentId").
foreignField("agent_id").
as("activities");
Aggregation aggregation = Aggregation.newAggregation(convertId,activityOperation);
return mongoTemplate.aggregate(aggregation, "agents", AgentDTO.class).getMappedResults()
但是,由于类型问题,它不会返回任何记录。是否可以在ProjectionOperation中实现$ toString或$ convert?或还有什么其他选择?
答案 0 :(得分:0)
我能够按照MongoDB $aggregate $push multiple fields in Java Spring Data
所述,通过用Java代码编写本机mongodb聚合操作来解决此问题实施此解决方案后,我能够如下添加本机$ addfields:
AggregationOperation addField=new GenericAggregationOperation("$addFields","{ \"agId\": { \"$toString\": \"$_id\" }}");