You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the field does not exist, $inc creates the field and sets the field to the specified value.
In Mongoose, $inc operations on non-existing fields result in correct data in the database, however, the field in Mongoose document will become NaN because currentValue is undefined.
importmongoosefrom'mongoose';constschema=newmongoose.Schema({count: Number,// no default value, not required});constModel=mongoose.model('IncTest',schema);awaitModel.collection.drop();constdoc=newModel({// optional 'count' field no set});awaitdoc.save();doc.$inc('count',1);awaitdoc.save();// doc.count is expected to be 1.// But instead, it is NaN
Expected Behavior
No response
The text was updated successfully, but these errors were encountered:
Hybrid-Force
changed the title
$inc on non-existing field resulting in NaN
$inc on non-existing field results in NaN
Sep 16, 2022
Prerequisites
Mongoose version
6.6.1
Node.js version
16.15.0
MongoDB server version
4.2.1
Description
According to MongoDB document on
$inc
,In Mongoose, $inc operations on non-existing fields result in correct data in the database, however, the field in Mongoose document will become
NaN
becausecurrentValue
is undefined.mongoose/lib/document.js
Lines 1738 to 1740 in 586c86f
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: