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

}

7
  • What about your code isnt working? Are you getting an error in the console? Commented Sep 29, 2016 at 14:20
  • @thanksd No error, it just doesn't save the new field of sensor Commented Sep 29, 2016 at 14:23
  • I think the answer is here stackoverflow.com/questions/7714216/… Commented Sep 29, 2016 at 14:24
  • 2
    @J.Kamans as per your sample data. there is no state with true value, thats why your if section not updating new field. Commented Sep 29, 2016 at 14:36
  • 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. Commented Oct 4, 2016 at 15:10

1 Answer 1

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.
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.