Home > Enterprise >  $unset not working in node (Mongoose) while working in mongosh
$unset not working in node (Mongoose) while working in mongosh

Time:05-10

how come wouldn't Mongoose and Mongosh return the same answer?
In the Schema, I have user ID and IoT device (smart light). Goal that I am trying to achieve is to remove completely first item in "smart_light" section.
Using Mongosh code works fine, but Mongoose is not removing the item. No errors are thrown, just "{acknowledged: false}". Mongoose is used with Node.

Schema (I am using replica set):

{
"UserID": "6276a2a6e59469e642801f4f",
"smart_light": {
    "1": {
        "Device_ID": "1",
        "Device_Details": {
            "Online_Status": true,
            "DeviceManufacturer": "Philips",
            "Model": "S-Li-7",
            "Serial_Number": "302504-519574",
            "Last_Update": {
                "Date": "2014-05-11 19:59:37",
                "Version": "V4",
                "Update_Pending": false
            },
            "Communication_Protocol_Set": [
                "Zigbee",
                "Zwave"
            ],
            "Spare_Parts": [
                "Bridge"
            ]
        },
        "Device_Status": {
            "Colour": "#08b8cd"
        }
    },
    "2": {
        "Device_ID": "2",
        "Device_Details": {
            "Online_Status": true,
            "DeviceManufacturer": "Philips",
            "Model": "S-Li-5",
            "Serial_Number": "136985-212439",
            "Last_Update": {
                "Date": "2011-08-16 03:45:29",
                "Version": "V2",
                "Update_Pending": true
            },
            "Communication_Protocol_Set": [
                "Zwave"
            ],
            "Spare_Parts": [
                "Bridge"
            ]
        },
        "Device_Status": {
            "Colour": "#4bf14b"
        }
    }
}

}

Mongosh:

db.iot_customer_devices.updateOne(
            {UserID: "6276a2a6e59469e642801f4f"}, 
            {"$unset":{"smart_light.1":""}});

Mongoose:

IoT_Customer_Device.updateOne(
    {UserID: "6276a2a6e59469e642801f4f"}, 
    {$unset: {"smart_light.1":""}}, 
    { safe: true, multi:true },
    function(err, result) {
    if (err) {
      console.log(err);
    } else {
     console.log(result);
    }}
    );

=================================================================== Update1:

It seems that if I try to remove (with mongoose) "UserID", this works... But objects cannot be removed so far?

IoT_Customer_Device.findOneAndUpdate(
    { UserID: "6276a2a6e59469e642801f18" }, 
    {$unset: {"UserID":1}}, (err, res) => {
    console.log(res);
});

CodePudding user response:

This is now closed, answer has been found:

IoT_Customer_Device.updateOne(
    { UserID: "6276a2a7e59469e642801f86" },
    [
        { $unset: "smart_light.1" }
    ],
    (err, data) => {
        if (!err) {
            res.redirect('/devices/smart_light');
        }
        else { console.log('Error in device delete :'   err); }
    }
);

This is working as intended, found my inspiration here

  • Related