Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB adding and removing fields in array

I'm working on a project that uses MongoDB, but I've never worked with it. I understand that by using javascript, you can manipulate the database. I made a script that removes some fields and adds some others, but it doesn't work properly:

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);
});

When db.floor.save(doc) is called, my floor is saved. All tiles now have a field called tileType, but no sensor has a field called activated! All sensors still have state. I do not know what is not working, I'm using the same code for the tile and for the sensor. Please help.


This is the structure of the floor:
{
"_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")

}

like image 454
J. Kamans Avatar asked Sep 29 '16 14:09

J. Kamans


1 Answers

Per comment of @sumitchoudhary:

When checking with if(sensor.state), it checks if the value of sensor.state is truthy. All my sensor states are false. So I never get in the if-statement... It is different for if(tile.nodeType), because that has a String in it and is always truthy (for my values).


The right check needs to be:
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.
}
like image 78
J. Kamans Avatar answered Oct 15 '22 10:10

J. Kamans