1

I'm kinda beginner with MongoDB and got task to push new values to Database, but only values to make it as a array, the thing is that current query duplicate everything, value and key, here is example:

"battery_message" : [
                {
                        "ChargerState" : null
                },
                {
                        "ChargerStatus" : null
                },
                {
                        "OutputVoltage" : null
                },
                {
                        "timestamp" : 1561442271.976242
                },
                {
                        "ChargerState" : null
                },
                {
                        "ChargerStatus" : null
                },
                {
                        "OutputVoltage" : null
                },
                {
                        "timestamp" : 1561442302.637065
                }
        ],

The thing is that probably some new date might appear in battery message so in my schema it looks like this:

battery_message: Array

And here is my query:

deviceControllers.updateDevice(
            {device_id: message.device_id},
            {$push: { battery_message: [
                {ChargerState: message.data.ChargerState},
                {ChargerStatus: message.data.ChargerStatus},
                {OutputVoltage: message.data.OutputVoltage},
                {timestamp: message.data.timestamp},
            ]}}
        )

Basically the idea is to make later for example ChargerState: [value, value, value, etc.]

1 Answer 1

2

You can use the $addToSet with $each to update the existing array. Here $addToSet operator adds or appends a value to an array, only if the value does not exist in the array.

So, your query might look something like:

deviceControllers.updateDevice(
        {device_id: message.device_id},
        {$addToSet: { battery_message:
        {
            $each: [
                   message.data.ChargerState,
                   message.data.ChargerStatus,
                   message.data.OutputVoltage,
                   message.data.timestamp
            ]
        }}})

Update 1 if battery_message have properties as :ChargerState:Array,ChargerStatus:Array,OutputVoltage:Array,timeStamp:Array

deviceControllers.updateDevice(
{device_id: message.device_id},
{$addToSet: { 
    battery_message.ChargerState:{$each:[value1,value2,,,,]},
    battery_message.ChargerStatus:{$each:[value1,value2,,,,]},
    battery_message.OutputVoltage:{$each:[value1,value2,,,,]},
    battery_message.timestamp:{$each:[value1,value2,,,,]}
}
})

Here if the key(s) ex:battery_message.ChargerState does not exist it will be created.

Note:

  • Just make sure that the key battery_message is an object and not an array and if the key(s) are meant to be separate individual key instead of nested-key please remove the battery_message key from the command.
  • If only a single entry has to be appended to an array than your query would look something like: $addToSet:{battery_message.ChargerState: message.data.ChargerState}
Sign up to request clarification or add additional context in comments.

4 Comments

Works well my friend but the thing is that it put only values now, later it will be hard to distinguish which value belongs to what property, so i wanted to have each Charger stuff as a separate array, that way data would land in array it belongs to. is it possible to make it done without changing Schema? Or to achieve that i would need to change schema to battery_message: { ChargerState: Array, ChargerStatus: Array} etc.? EDIT, turned out it doesn't work well as now it is stored in one array, so it means that state status and voltage and mixing with each other, not kept in separate fields
@Zirek, in that case, the $addToSet will have multiple objects with their respective values in each statement. anyway, i have updated my answer.
it works perfectly fine now! thank you so much for your help:) only it is not battery_message.ChargerState:{$each:[value1,value2,,,,]} but needs to be with quotes otherwise it gives error, "battery_message.ChargerState":{$each:[value1,value2,,,,]} :)
Yes, sorry for that, if the path to the key is defined, have to use a string instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.