Ok, so this one is a bit weird. According to docs $$NOW
is supposed to resolve into current datetime. I have $jsonSchema validation that contains:
"offlineSince": {
"bsonType": "date"
},
If I try to use this command in mongosh I get an error:
> db.user.meta.updateOne({_id:'652656'}, { $set: {isOnline:true}, $setOnInsert: {schema:1, offlineSince:'$$NOW', dms:{ids:[]}, dmPts:0, groups:{ids:[]}, channels:{ids:[]}} }, {upsert:true})
MongoServerError: Document failed validation
But at the same time the following works:
> db.user.meta.updateOne({_id:'652656'}, { $set: {isOnline:true}, $setOnInsert: {schema:1, offlineSince:new Date(), dms:{ids:[]}, dmPts:0, groups:{ids:[]}, channels:{ids:[]}} }, {upsert:true})
{
acknowledged: true,
insertedId: '652656',
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 1
}
The only difference is use of new Date()
instead of $$NOW
. Why is this happening?