Node js mongodb updating array with index in variable

I have a mongoDB document, which, when in object form, you would access the desired array like this:

map.layers.1.data.INDEX

And this is what I currently have:

//Where = index, to = change to this
selector["data.layers.1.data."+where];
mDB.update({"_id":msg.island+msg.map}, {$set:{selector:to}} , function(err, doc) {
    console.log(doc);
});

(I made my map with Tiled.) Where data is an array, and I want to update just that one array index. But that index has to come from a variable. Is there a way to do this in mongoDB?

I'm currently reading whole document then replacing wanted data and then updating whole document. And that's not good, fast or necessary(I hope).

Not sure I understood your description - an example document would be helpful - but I'll wing it. Assuming your documents look like

{
    "data" : {
        "layer" : [
            { "data" : [ 2, 5] },
            { "data" : [ 99 ] },
            { "data" : [ 0, 1, 2, 3 ] },
            { "data" : [ -1, "truffles", 6 ] },
        ]
    }
}

then you could update a fixed index of the array data.layer.2.data stored as the value of a variable myIndex like so in the mongo shell (similar to node but synchronous, no callback)

var myIndex = complicatedprocedureforgeneratingtheindex() // myIndex = 1
var mySet = {}
mySet["data.layer.2.data." + myIndex] = to
db.map.update(myQuery, { "$set" : mySet })
db.map.find(myQuery)
{
    "data" : {
        "layer" : [
            { "data" : [ 2, 5] },
            { "data" : [ 99 ] },
            { "data" : [ 0, 99, 2, 3 ] },
            { "data" : [ -1, "truffles", 6 ] },
        ]
    }
}

Roughly (i.e. ideas are there but no copy paste b/c of undefined variables)

This is exactly the answer!

In order to update an array with an given index, you have to define an object seperately, before the update:

set = {};

and then specify the index as a property of the object with [] embraces.

set['example.nested.object.' + i] = foo; 

Then just update your MongoDB

Collection.update($set: set);

Should work.