M
M
Mark Adams2018-08-01 11:05:32
MongoDB
Mark Adams, 2018-08-01 11:05:32

What is the correct way to use dynamic field names in MongoDB?

Surely the question is not immediately clear.
I have the following query to change data:

db.collection(history).updateOne(
    {user:req.body.user.toLowerCase()}, 
    {$set:{<filed_name>:0}}, 
    function (err, docs) {
        if (err) {console.log(err); return false}
        console.log('[docsModified: ' + docs.result.nModified + ']');
})

It is necessary to make sure that `filed_name` can change depending on the function parameter, i.e. I want to set the name of the field to be changed. In fact, there is a user base that has balances of various currencies, and so I need to reset the balance by the name of the currency (field).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question