question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with aggregation LookupOperation - when we use _id vs id as foreign key

See original GitHub issue

If 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:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
n4kongcommented, Jul 25, 2022

@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

0reactions
sbg-shcommented, Aug 19, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found