1

How can I reference two mongodb collections using spring data while the localField is of type ObjectId and foreignField is of type String?

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()

However, this doesn't return any records because of the type issue. Is it possible to implement $toString or $convert in ProjectionOperation? or what other options are there?

Feras Odeh
  • 9,136
  • 20
  • 77
  • 121

1 Answers1

0

I was able to solve it by writing native mongodb aggregation operation in java code as described in MongoDB $aggregate $push multiple fields in Java Spring Data

After implementing this solution I was able to add native $addfields as follows:

AggregationOperation addField=new GenericAggregationOperation("$addFields","{ \"agId\": { \"$toString\": \"$_id\" }}");
Feras Odeh
  • 9,136
  • 20
  • 77
  • 121