Issue with aggregation LookupOperation - when we use _id vs id as foreign key
See original GitHub issueIf you have following spring mongo data entities:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Entity1 {
@Id
private String id;
}
and this entity :
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Entity2 {
@Id
private String id;
private List<Entity1> entities1;
}
public class Entity2Full extends Entity2Full
Then following do NOT work:
LookupOperation lookup = LookupOperation.newLookup()
.from("entity1")
.localField("entities1._id")
.foreignField("_id")
.as("entities1Full");
....
MatchOperation matchOperation = ...
Aggregation agg = Aggregation.newAggregation(matchOperation, lookup);
List<Entity2Full> user = mongoTemplate.aggregate(agg, Entity2.class, Entity2Full.class).getMappedResults(); // query Entity2 and link with Entity1
If I change the LookupOperation to :
LookupOperation lookup = LookupOperation.newLookup()
.from("entity2")
.localField("entities1.id") //CHANGE _id to id
.foreignField("id") //CHANGE _id to id
.as("entities1Full");
Then it works.
E.g. changing “_id” to “id” in LookupOperation will make the above working - the issue is that on real mongo the _id works and id do not as it should be.
I have tried to put
import org.springframework.data.mongodb.core.mapping.Field;
@Field(name = "_id")
@Id
private String id;
on the id but this did not solve the issue - only when I put id and not _id it will work
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Mongodb Join on _id field from String to ObjectId
You can use $toObjectId aggregation from mongodb 4.0 which converts String id to ObjectId db.role.aggregate([ { "$lookup": { "from": "user", ...
Read more >$lookup (aggregation) — MongoDB Manual
Starting in MongoDB 5.1, $lookup works across sharded collections. To combine elements from two different collections, use the $unionWith pipeline stage.
Read more >MongoDB Lookup Aggregations: Syntax, Usage & Practical ...
MongoDB provides its users with the ability to aggregate the data they want, as long as it is present in the same database....
Read more >Lookup in MongoDB - YouTube
Hello and namaste everyone,Today, we are continuing with the topic of $lookup in MongoDB. In this video, we will talk about how to...
Read more >$lookup Examples | MongoDB - StackChief
Depending on which type of query you run, $lookup will take slightly different parameters (let, pipeline vs localField, foreignField). For more on the ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jchobantonov-runbuggy Hi, i got same issue, and resolved by change mongodb version in the test application.properties
spring.mongodb.embedded.version=4.0.2
Hello @n4kong , I have the same issue but it does not work for me, could you please tell more details.