Question: Want to upsert in object properties in a array of a document 

Consider a document in collection m 

```
{ "_id" : ObjectId("524bfc39e6bed5cc5a9f3a33"), 
 "x" : [       
    { "id":0.0, "name":"aaa"},{ "id":1.0, "name":"bbb"}  
 ]
}

```

Want to add `age:100` to `{ "id":0.0, "name":"aaa"}` .
Not just age .. But but provision for upsert in the array element {}. So it can contain `{age:100,"city":"amd"}` (since i am getting this from the application service)

Was trying this... But did not worked as it replaced the entire array element

```
db.m.update({_id:ObjectId("524bfc39e6bed5cc5a9f3a33"),
     "x" : {
                "$elemMatch" : {
                        "id" : 0.0
                }
}},
{
        $set : {
                "x.$" : {
                    "age": 100
            }
        }
},
{upsert: true}
)

```

Changed the document to (which i did not wanted) 

```
 { "_id" : ObjectId("524bfc39e6bed5cc5a9f3a33"), 
     "x" : [       
        { "age":100},{ "id":1.0, "name":"bbb"}  
     ]
    }

```

Is this possible without changing schema.

Answer: 
```
$set : {"x.$" : {"age": 100}}

```

`x.$` sets the entire matched array element to `{age: 100}`

This should work:

```
db.m.update({_id:ObjectId("524bfc39e6bed5cc5a9f3a33"),
"x.id": 0.0}, {$set: {"x.$.age": 100 }});

```

Using `elemMatch`: 

```
db.test.update({x: {$elemMatch: {id: 1}}},{$set:  {"x.$.age": 44}})

```

Note that the `upsert` option here, is redundant and wouldn't work if the `id` isn't present in `x` because the positional operator `$` doesn't support `upserting`.
This is not possible without changing schema. If you can change schema to use an object to store your items (rather than an array), you can follow the approach I outlined in [this answer](https://stackoverflow.com/questions/8871363/upsert-array-elements-matching-criteria-in-a-mongodb-document/21347039#21347039).