My problem is I want to push data on to a specified position in an array in a MongoDb Document, but when I push the new data it doesn't add it to the right position.
When run this update statement:
collection.update({ "schraenke.name": "Schrank1" }, {
$push:
{
"schraenke": [{
"masterbricks": {
"sensor1": { value: "test" }
}
}]
}
});`
The result from the above update is the document:
{
"_id": "55599876095c6bac18209dfa",
"name": "Serverraum1",
"schraenke": [
{
"name": "Schrank1",
"nummer": "1",
"reihe": "1",
"masterbricks": {
"name": "Mastrebrick2.0",
"uid": "6dKiRt",
"he": "1-20",
"sensor1": {
"name": "Temperatur",
"uid": "qvf"
}
}
},
[
{
"masterbricks": {
"sensor1": {
"value": "test"
}
}
}
],
]
}
The value field should be added to "sensor1", but it adds a new array.
My expected result is:
{
"_id": "55599876095c6bac18209dfa",
"name": "Serverraum1",
"schraenke": [
{
"name": "Schrank1",
"nummer": "1",
"reihe": "1",
"masterbricks": {
"name": "Mastrebrick2.0",
"uid": "6dKiRt",
"he": "1-20",
"sensor1": {
"name": "Temperatur",
"uid": "qvf",
"value" : "test"
}
}
},
]
}
What am I doing wrong?
valuefield be an array or just a single value?sensordocument must have avaluefiled which is an array of measured values?