MorphiaOrg / MorphiaOrg/morphia

Calling find for reference object when save even ignoreMissing is set to true.

Open
#4,236 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Java
Stars
1.7k
Forks
449
Avg merge
7m
Merged PRs (30d)
10

Description

When calling save to an entity with reference. it would call a find before update.
If that's a reference in a list then it causes an N+1 issue.

example:

@Entity(value = "test1", useDiscriminator = false)
public class Test1 {
    @Id
    ObjectId _id;
    String name;
    List<EmbeddedTest2> embeddedTest2s;
...
}

@Entity(useDiscriminator = false)
public static class EmbeddedTest2{
    Integer seq;
    @Reference(idOnly = true, lazy = true, ignoreMissing = true)
    Test2 test2;
...
}

@Entity(value = "test2", useDiscriminator = false)
public class Test2 {
    @Id
    ObjectId _id;
    String name;
...
}

when calling datastore.save(test1). it will call 3 extra time of find "test2" if there are 3 entries.
would it be possible to group it in to 1 call with _id in [...] or just stop calling if ignoreMissing = true?

** Please complete the following information: **
Server Version: 8.0.20
Driver Version: 5.5.1
Morphia Version: 2.5.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the datastore.save(test1) path that processes @Reference fields, using the EmbeddedTest2 example with lazy and ignoreMissing enabled. Reproduce the save with three referenced Test2 entries and inspect the database calls. Done means the unnecessary per-reference find calls are avoided or consolidated, with behavior verified for missing references.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.