Skip to content

Repository lookups by DocumentReference returning empty results #4033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maxim04 opened this issue Apr 21, 2022 · 4 comments
Closed

Repository lookups by DocumentReference returning empty results #4033

maxim04 opened this issue Apr 21, 2022 · 4 comments
Assignees
Labels
type: bug A general bug

Comments

@maxim04
Copy link

maxim04 commented Apr 21, 2022

Using spring-boot-starter-data-mongodb:2.6.6

  • spring-boot-starter:2.6.6
  • mongodb-driver-sync:4.4.2
  • spring-data-mongodb: 3.3.3

I have the following code below with 2 documents, User and Product. Product has a DocumentReference to a User object. Using a repository for each document, lookups by id of type String work as expected. But lookups for Product based on the User DocumentReference using an id of type String return 0 results. However if I do the same lookup using an id of type ObjectId I get back the correct set of results.

I also discovered if I use @MongoId(FieldType.STRING) for the id property, the lookups for Product based on the User DocumentReference using an id of type String returns the correct results, but now lookups for the Product or User using id of type String returns empty results. To get back the right set of results I have to lookup Product or User with an id of type ObjectId, essentially the reverse behaviour of the first observation.

Is it supposed to behave like this? I would expect the same behaviour when looking up documents using an id of type String.

The code:

@SpringBootApplication
@EnableMongoRepositories
public class Demo1Application implements CommandLineRunner {

    @Autowired
    UserRepository userRepository;
    @Autowired
    ProductRepository productRepository;

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        userRepository.deleteAll();
        productRepository.deleteAll();

        User user = new User();
        userRepository.save(user);

        Product product = new Product();
        product.setOwner(user);
        productRepository.save(product);

        List products1 = productRepository.findAllByOwner_Id(user.getId()); // returns 0 results, wrong

        List products2 = productRepository.findAllByOwner_Id(new ObjectId(user.getId())); // returns 1 result, correct
    }
}

@Document
@Data
class User {
    @Id
    String id;
}

@Document
@Data
class Product {
    @Id
    String id;
    @DocumentReference
    User owner;
}

interface UserRepository extends MongoRepository<User, String> {

}
interface ProductRepository extends MongoRepository<Product, String> {
    List<Product> findAllByOwner_Id(String id);
    List<Product> findAllByOwner_Id(ObjectId id);
}
@christophstrobl
Copy link
Member

While we're looking at the issue, using the target object as the reference should work. In this case the given User is considered the source for the pointer referencing the target document.

 List<Product> findAllByOwner(User owner);

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels May 6, 2022
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label May 13, 2022
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels May 20, 2022
@maxim04
Copy link
Author

maxim04 commented May 21, 2022

While we're looking at the issue, using the target object as the reference should work. In this case the given User is considered the source for the pointer referencing the target document.

 List<Product> findAllByOwner(User owner);

Thanks, in my case though I want to avoid the unnecessary lookup for the whole object since I know the id of the reference object I am interested in. I see there is a draft to fix this issue, thanks for that, looking forward to the fix.

@mp911de mp911de added this to the 3.4.3 (2021.2.3) milestone Sep 19, 2022
@mp911de mp911de added the type: bug A general bug label Sep 19, 2022
mp911de pushed a commit that referenced this issue Sep 19, 2022
mp911de pushed a commit that referenced this issue Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
4 participants