Reputation: 2465
My Mongoose Command
Modelname.update(
{$and :[ {'field1' : field1}, {'field2' : field2}, {'field3' : field3}, {'field4' : field4}]},
{
$set : {'timestamp' : timestamp},
$inc : {speed_string: 1}
},{upsert: true}, function (err, record)
{
if (err)
{
}
else
{
}
});
As you can see that speed_string is a variable which is supposed to get increased in the real document or get upserted . I have logged it just before the query and it is indeed a string (and not empty like the error reads) but the query is not going through and I am getting this error.
"errmsg":"'$inc' is empty. You must specify a field like so: {$inc: {: ...}}"}
When I am replacing that variable with 'expected_string'
things are working fine and that is more confusing, nevertheless.
There is something I am doing wrong, help is appreciated.
Upvotes: 1
Views: 702
Reputation: 311865
To use a variable as a field name, you need to use the computed property name syntax where the variable is enclosed in square brackets:
Modelname.update(
{$and :[ {'field1' : field1}, {'field2' : field2}, {'field3' : field3}, {'field4' : field4}]},
{
$set : {'timestamp' : timestamp},
$inc : {[speed_string]: 1}
},{upsert: true}, function (err, record)
{
...
});
Also, multiple terms in a query are implicitly ANDed, so you can simplify your query object to:
Modelname.update(
{
'field1' : field1,
'field2' : field2,
'field3' : field3,
'field4' : field4
},
{
$set : {'timestamp' : timestamp},
$inc : {[speed_string]: 1}
},{upsert: true}, function (err, record)
{
...
});
Upvotes: 1