Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your last query fails in the mongo driver in a sense that the update has no effect - but it's not an actual error. Just let me reproduce what you are doing in the mongo shell:</p> <pre><code>&gt; db.coll.insert({_id:1,name:"name1"}) &gt; db.coll.update({name:"name1"}, {"$push": {dependents: {name:"a", age:50}}}) &gt; db.coll.update({name:"name1"}, {"$push": {dependents: {name:"b", age:55}}}) &gt; db.coll.findOne() { "_id" : 1, "dependents" : [ { "name" : "a", "age" : 50 }, { "name" : "b", "age" : 55 } ], "name" : "name1" } &gt; db.coll.update({"dependents.name": "a"}, {"$push": {"dependents.dep_children": {name:"aa"}}}) can't append to array using string field name: dep_children &gt; db.coll.update({"dependents.name": "a"}, {"$push": {"dependents.$.dep_children": {name:"aa"}}}) &gt; db.coll.findOne() { "_id" : 1, "dependents" : [ { "age" : 50, "dep_children" : [ { "name" : "aa" } ], "name" : "a" }, { "name" : "b", "age" : 55 } ], "name" : "name1" } </code></pre> <p>Unfortunately, I have very little experience with the native mongo java driver (I'm usually on Spring data), but changing your line</p> <pre><code>updatekid.put("$push", new BasicDBObject("dependent.dep_children",tenantkid)); </code></pre> <p>to</p> <pre><code>updatekid.put("$push", new BasicDBObject("dependent.$.dep_children",tenantkid)); </code></pre> <p>should do the trick as well.</p> <p>The reason for that behavior is that "dependent.dep_children" is not a valid selector as it corresponds to "go to field dep_children within the subdocument dependent". However, "dependent" happens to be an array without any fields. The $ replaces an explicit index and will make sure the correct subdocument from your query is selected.</p> <p>Also see <a href="https://stackoverflow.com/questions/6758339/updating-embedded-document-property-in-mongodb?rq=1">here</a> for a less error-prone formulation of your query - without using $elemMatch it will only work if your query uniquely identifies a certain array element. With $elemMatch it will always work, but only update the first match.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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