I have two arrays, one contain keys and other the values. I want to insert key value pairs in mongodb.
var keys = ["item1","item2","item3"];
var values = ["15","14","19"];
I am using insertOne to enter the key value pairs into the database.
MongoClient.connect('mongodb://localhost:27017/Clients', (err,db) => {
if(err)
{
return console.log('Unable to Connect');
}
console.log('Connected to Mongodb server');
for(var i=0,l=keys.length; i<l;i++)
{
db.collection('Orders').insertOne({
keys[i] : values[i]
}, (err,result) => {
if(err)
{
return console.log(err);
}
});
}
db.close();
});
**
**
Error: keys[i] : values[i]
^
SyntaxError: Unexpected token [
**
**
Where am I wrong? Is there any other way to complete this task ? Any help is appreciated.