Use a notation bracket to dynamically build a document. First you must create an empty object that will hold the keys, and then use parenthesis notation to add dynamic fields to the object:
insertData_dynamic_colone: function(collection, colone1, colone2) {
var obj = {};
obj[colone1] = "14";
obj[colone2] = "15";
dbObject.collection(collection).insertOne(obj, function(err, result) {
assert.equal(err, null);
});
}
or
insertData_dynamic_colone: function(collection) {
var obj = {},
colone1 = "prod",
colone2 = "prod2";
obj[colone1] = "14";
obj[colone2] = "15";
dbObject.collection(collection).insertOne(obj, function(err, result) {
assert.equal(err, null);
});
}
ES2015 Object initializer ( @xmikex83):
insertData_dynamic_colone: function(collection) {
var colone1 = "prod";
var colone2 = "prod2";
dbObject.collection(collection).insertOne({
[colone1] : "14",
[colone2] : "15"
}, function(err, result) {
assert.equal(err, null);
});
}