I am using nodejs
npm mongodb
mongodb version: mongodb@3.1.9
nodejs version: v10.11.0
Here is the document layout:
{
"_id": {
"$oid": "5c1bc140a3902a10542df7a6"
},
"email": "radoslav.k.marrinov@gmail.com",
"username": "rikotech",
"deviceId": "FA661234A511",
"password": "12345",
"devices": [
{
"_id": {
"$oid": "5c1bb980fb6fc00eee83b4d9"
},
"id": "FA661234A511",
"class": "11",
"name": "someName",
"displayName": "disName",
"endpoints": [
{
"class": "binarySwitch",
"name": "bs1",
"displayName": "sw1DisplName"
},
{
"class": "binarySwitch",
"name": "bs2",
"displayName": "sw2DisplName"
}
]
}
]
}
I want to change the value of devices[0].endpoints[0].displayName: "newName"
I am able to locate the document having the device.id
and endpoint.displayName
This query finds the document:
{
devices: { $elemMatch: { id: "FA661234A511" } }
}
I know I should be using update
method but I can not figure out the how to select the field to update?
I have to find the first occurrence of device
with "id": "FA661234A511"
and then the first occurrence of endpoint
with "displayName": "sw1DisplName"
id
is unique in the scope of devices
and displayName
is unique in the scope of endpoints
I tried whit this:
update({
devices: {
$elemMatch: {
id: "FA661234A511",
endpoints: { $elemMatch: { displayName: "sw1DisplName" } }
}
}
}, {
"devices.$.endpoints.$.displayName": "diplayRiko"
})
Doesn't work :(.
I get exception:
MongoError: Too many positional (i.e. '$') elements found in path 'devices.$.endpoints.$.displayName'