8

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")

}

J. Kamans
  • 575
  • 5
  • 17
  • What about your code isnt working? Are you getting an error in the console? – thanksd Sep 29 '16 at 14:20
  • @thanksd No error, it just doesn't save the new field of sensor – J. Kamans Sep 29 '16 at 14:23
  • I think the answer is here http://stackoverflow.com/questions/7714216/add-new-field-to-a-collection-in-mongodb – thanksd Sep 29 '16 at 14:24
  • @thanksd When updating a tile, my new field is saved. When updating a sensor, the new field is not saved and the old field is used. Why does that happen? – J. Kamans Sep 29 '16 at 14:30
  • 2
    @J.Kamans as per your sample data. there is no state with true value, thats why your if section not updating new field. – Sumit Kumar Sep 29 '16 at 14:36
  • @sumitchoudhary TY! You are right! It's the JavaScript truthy/falsey thing. I thought `if(sensor.state)` checks if the field exists, but it checks the value of `sensor.state` for falsey/truthy. – J. Kamans Sep 29 '16 at 14:42
  • 6
    @sumitchoudhary: The comments section is for comments. If you have the answer to the question, _post it as an answer please_. Certainly don't ask those who do post answers to convert their answers to comments (?!). Thanks. – Lightness Races in Orbit Oct 04 '16 at 15:10

1 Answers1

7

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.
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
J. Kamans
  • 575
  • 5
  • 17