Note that there are some explanatory texts on larger screens.

plurals
  1. POMorphia: Search for null values in a list of embedded objects
    primarykey
    data
    text
    <p>I have a list of embedded entities:</p> <pre><code>@Embedded private List&lt;EmbeddedEntity&gt; embedded = new ArrayList&lt;EmbeddedEntity&gt;(); </code></pre> <p>In this list, I want to search for any embedded entity which has a specific attribute (let's call it <code>foo</code>), but not another one (<code>bar</code>). So <code>foo</code> should be non-null and <code>bar</code> null in Java / inexistant in MongoDB.</p> <p>I tried the following code (I do have the UUID of the <code>Entity</code> containing the list):</p> <pre><code>Query&lt;Entity&gt; query = mongoDataStore.find(Entity.class).field("uuid").equal(uuid) .field("embedded.foo").exists() .field("embedded.bar").doesNotExist(); </code></pre> <p>This works correctly if the list is empty or has a single entry (where <code>foo</code> has a value and <code>bar</code> does not yet exist). But as soon as any <code>bar</code> attribute has a value, the query returns the wrong result. So I'm looking for a query, which iterates through all the embedded entities and fires on any missing <code>bar</code>. Is that possible?</p> <p>Example data:</p> <pre><code>// the query does not pick up the entity as it doesn't have a foo - // that's what I want { uuid: "...", [ { embedded: } ] } // the query picks up the entity as there is a foo but no bar - // that's what I want { uuid: "...", [ { embedded: { foo: date } } ] } // the query does not pick up the entity - that's not what I want // as one foo has a value and its bar doesn't { uuid: "...", [ { embedded: { foo: date, bar: date } }, { embedded: { foo: date } } ] } </code></pre> <p>PS: I get the same result with <code>.field("embedded.bar").hasThisOne(null)</code>.</p> <p>PPS: Manually iterating through the list elements is not really an option, as I want to use the query for an update operation.</p> <p>PPS: I think this is a bug in Morphia - see my answer below (<a href="https://stackoverflow.com/a/9705175/573153">https://stackoverflow.com/a/9705175/573153</a>) for a workaround</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload