1

So im attempting to push a different index per array into a $push call to populate a mongodb server however Im having trouble with making the call specific to the array that I am attempting to access

There is an inital array that is set for IP addresses and Array names in the database that they need to be set to

//Team IP Info
var teamIps = [
    '8.8.8.8',
    '8.8.8.9',
    '8.8.8.8',
    '8.8.8.8',
    '8.8.8.8',
]
var boxNames = [
    'Linux1',
    'Linux2',
    'Windows1',
    'Windows2',
    '98',
]

This is the code for the $push call

for (let index = 0; index < teamIps.length; index++) {
    var hostIn = teamIps[index];
    const boxName = boxNames[index];
    const liTest = 'services.0.ICMP_Linux1'
    var db_base = 'services.0.ICMP_';
    var db_index = db_base.concat(boxName);
    console.log('Inital : ' + boxName)

    //This should work and makes life easier but I cant append the db_index string into the mongoose push call

    var result = await session.pingHost(hostIn, function (error, hostIn, sent, rcvd) {
        var ms = rcvd - sent;
        var db_base = 'services.0.ICMP_';
        var db_index = db_base.concat(boxName).toString();
        console.log(typeof db_index)
        if (error){
            var output = hostIn + ": " + error.toString()
            Team.findOneAndUpdate(
                { name: name }, 
                {$push: {db_index :{ timeStamp: epochTime , status: false , error: error.toString()} }},
                function(err,suc){
                    if(err){
                        console.log(err)
                    }
                    else{
                        console.log(hostIn + ' : ' + db_index + " : " + output)
                    }
                });
            // res.send({
            //     result: output
            // })
        }
        else{
            var output = hostIn + ": Alive (ms=" + ms + ")"
            Team.findOneAndUpdate(
                { name: name }, 
                {$push: {db_index :{ timeStamp: epochTime , status: true , speed: ms} }},
                function(err,suc){
                    if(err){
                        console.log(err)
                    }else{
                        console.log(hostIn + ' : ' + db_index + " : " + output)
                    }
                });
            // res.send({
            //     result: output
            // })
        }
    });

In this call

{$push: {db_index :{ timeStamp: epochTime , status: false , error: error.toString()} 

I would like to be able to change the db_index to each in the array however it always defaults to the actual word db_index

I cant seem to figure out how to pass a string into this call without it failing

A call that works looks something like this

{$push: {'services.0.ICMP_Linux1':{ timeStamp: epochTime , status: false , error: error.toString()} }},

Everything works database side and when manually passed with this call it works.

1 Answer 1

1

You can use computed property names:

{$push: {[db_index] :{ timeStamp: epochTime , status: false , error: error.toString()} } }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is the solution I search docs for hours and couldn't find it. Just brackets [ ] -_-

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.