gpt4 book ai didi

javascript - MongoDB在数组中添加和删除字段

转载 作者:IT老高 更新时间:2023-10-28 13:15:41 25 4
gpt4 key购买 nike

我正在开发一个使用 MongoDB 的项目,但我从未使用过它。我知道通过使用 javascript,您可以操作数据库。我制作了一个脚本,删除了一些字段并添加了一些其他字段,但它无法正常工作:

db.floor.find({_id:"003"}).forEach( function(doc) { // Find floor with id = 003.

var tiles = doc.tiles; // Get tiles from floor.

for(var i = 0; i < tiles.length; i++){ // Loop through tiles.
var tile = tiles[i]; // Get tile at index i.
if (tile.nodeType){ // If tile has a field "nodeType",
tile.tileType = tile.nodeType; // add a field "tileType"
delete tile.nodeType; // and delete the "nodeType" field.
}

// THIS CODE BELOW IS NOT WORKING
var sensors = tile.sensors; // Get sensors of tile.
for(var j = 0; j < sensors.length; j++){ // Loop through sensors.
var sensor = sensors[j]; // Get sensor at index j.
if (sensor.state){ // If sensor has a field "state",
sensor.activated = sensor.state; // add a field "activated"
delete sensor.state; // and delete the "state" field.
}
}
}
db.floor.save(doc);
});

db.floor.save(doc) 被调用时,我的楼层被保存了。所有图 block 现在都有一个名为 tileType 的字段,但没有传感器有一个名为 activated 的字段!所有传感器仍然有state。我不知道什么不起作用,我对瓷砖和传感器使用相同的代码。请帮忙。


这是地板的结构:

{
"_id" : "003",
"_class" : "nl.kamans.Floor",
"name" : "FloorDemo",
"tiles" : [
{
"_id" : "035",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 0,
"y" : 0,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "042",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 1,
"y" : 0,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "049",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 2,
"y" : 0,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "007",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 3,
"y" : 0,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "029",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 0,
"y" : 1,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "021",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 1,
"y" : 1,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
},
{
"_id" : "014",
"sensors" : [
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
},
{
"state" : false,
"ignored" : false
}
],
"x" : 2,
"y" : 1,
"batteryVoltage" : 0,
"orientation" : "NORTH",
"nodeType" : "WIRELESS"
}
],
"height" : 1,
"width" : 3,
"defined" : true,
"lastUpdate" : ISODate("2016-09-29T13:26:40.866Z")

}

最佳答案

根据@sumitchoudhary 的评论:

使用if(sensor.state)检查时,检查sensor.state的值是否为真。我所有的传感器状态都是错误的。所以我从来没有进入 if 语句... if(tile.nodeType) 是不同的,因为它有一个字符串并且总是真实的(对于我的值(value)观)。


正确的检查需要是:

if ("state" in sensor)
{
// If sensor has a field "state",
sensor.activated = sensor.state; // add a field "activated"
delete sensor.state; // and delete the "state" field.
}

关于javascript - MongoDB在数组中添加和删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39772718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com