Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you have a conceptual problem with your schema. Remember that you always query for top level documents. The reason your query isn't working is because you're asking it to return <strong>top level</strong> documents where <strong>at least one</strong> of the elements has foo and <strong>at least one</strong> of the elements does not have a bar value. Note that both conditions need not apply on the same array element.</p> <p>You can do what you want in MongoDB using a $elemMatch :</p> <p><code>find({embedded:{$elemMatch:{foo:{$exists:true}, bar:{$exists:false}}}})</code> as demonstrated here :</p> <pre><code>&gt; db.test.save({embedded:[]}) &gt; db.test.save({embedded:[{foo:1}]}) &gt; db.test.save({embedded:[{bar:1}]}) &gt; db.test.save({embedded:[{foo:1, bar:1}]}) &gt; db.test.find({embedded:{$elemMatch:{foo:{$exists:true}, bar:{$exists:false}}}}) { "_id" : ObjectId("4f60c4d56fa40267a11d2f2c"), "embedded" : [ { "foo" : 1 } ] } </code></pre> <p>If "null" is a valid value for bar you can simply change it to :</p> <pre><code>&gt; db.test.save({embedded:[{foo:1, bar:null}]}) &gt; db.test.find({embedded:{$elemMatch:{foo:{$exists:true}, $or:[{bar:{$exists:false}}, {bar:null}]}}}) { "_id" : ObjectId("4f60c4d56fa40267a11d2f2c"), "embedded" : [ { "foo" : 1 } ] } { "_id" : ObjectId("4f60c52a6fa40267a11d2f30"), "embedded" : [ { "foo" : 1, "bar" : null } ] } </code></pre> <p>Now, in Morphia the $elemMatch is wrapped by the FieldEnd method "hasThisElement". I'm not that familiar with Morphia (I wrote and use my own mapper) but this should take a DBObject with the above clause as it's value and that should result in what you need to do. </p> <p>But again, this will return the top level documents that have elements in their embedded array that match these criteria. If you only want to return the matching elements you may have to turn your embedded structure in a top-level collection. It will be sufficient if your update only involves modifying the matching element(s) through the $ positional operator though :</p> <pre><code>db.test.update( {embedded:{$elemMatch:{foo:{$exists:true}, $or:[{bar:{$exists:false}}, {bar:null}]}}}, {$set:{'embedded.$.bar':"yay!"}} ) </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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