I want build a simple function which can update (upsert
) a document by collection name, field key, field value, updateData
example:
function updateDB(tableName, id, rowInfo, checkfield, callback) {
db.collection(tableName, function (err, collection) {
if (err) {
console.log(err);
} else {
console.log(checkfield);
collection.update({ checkfield: id }, rowInfo, { upsert: true }, function (err, objects) {
if (err) {
throw err;
callback(false);
} else {
// console.log(objects);
callback(true);
}
});
}
});}
However when I use parameter as field filter in update query it always inserts new data an does not update the document. Example:
collection.update({ checkfield: id }.....
But when I change the field filter to realFieldName
it works correctly. Example:
collection.update({ 'realFieldName': id }....
What is the difference between the two?