$unset 在 mongosh 中工作时无法在节点(猫鼬)中工作
$unset not working in node (Mongoose) while working in mongosh
为什么 Mongoose 和 Mongosh return 的答案不一样?
在架构中,我有用户 ID 和物联网设备(智能灯)。我要实现的目标是完全删除“smart_light”部分中的第一项。
使用 Mongosh 代码工作正常,但 Mongoose 没有删除该项目。没有错误被抛出,只是“{acknowledged: false}”。 Mongoose 与 Node 一起使用。
架构(我正在使用副本集):
{
"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"
}
}
}
}
蒙哥什:
db.iot_customer_devices.updateOne(
{UserID: "6276a2a6e59469e642801f4f"},
{"$unset":{"smart_light.1":""}});
猫鼬:
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);
}}
);
=========================================== ======================
更新 1:
似乎如果我尝试删除(使用 mongoose)“UserID”,这是可行的...但是到目前为止无法删除对象?
IoT_Customer_Device.findOneAndUpdate(
{ UserID: "6276a2a6e59469e642801f18" },
{$unset: {"UserID":1}}, (err, res) => {
console.log(res);
});
已关闭,已找到答案:
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); }
}
);
这是按预期工作的,找到了我的灵感
为什么 Mongoose 和 Mongosh return 的答案不一样?
在架构中,我有用户 ID 和物联网设备(智能灯)。我要实现的目标是完全删除“smart_light”部分中的第一项。
使用 Mongosh 代码工作正常,但 Mongoose 没有删除该项目。没有错误被抛出,只是“{acknowledged: false}”。 Mongoose 与 Node 一起使用。
架构(我正在使用副本集):
{
"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"
}
}
}
}
蒙哥什:
db.iot_customer_devices.updateOne(
{UserID: "6276a2a6e59469e642801f4f"},
{"$unset":{"smart_light.1":""}});
猫鼬:
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);
}}
);
=========================================== ====================== 更新 1:
似乎如果我尝试删除(使用 mongoose)“UserID”,这是可行的...但是到目前为止无法删除对象?
IoT_Customer_Device.findOneAndUpdate(
{ UserID: "6276a2a6e59469e642801f18" },
{$unset: {"UserID":1}}, (err, res) => {
console.log(res);
});
已关闭,已找到答案:
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); }
}
);
这是按预期工作的,找到了我的灵感