This won’t fix your main issue, but I noticed that the default value here is static (defined at app start (different on server and client)) instead of generated as needed.
You should probably use autoValue to set it when the document is cleaned / created
Having a look at the error proper, it looks like it’s caused by this section in collection2, which applies to upserts:
Unfortunately the prediction about being error prone in the comment came true.
You can see that it’s cloning in the selector fields to the object being validated. The comment asserts that in an upsert with plain fields in the selector, those fields will get merged with the result of the modifier to be the new document. Not sure if that’s accurate, but it’s why simple-schema is doing this.
Thinking about it, I’m not sure an upsert makes sense when the selector is a range of dates. What would date would the inserted document use if none are found?
Actually, That’s probably why you have a defaultValue in the schema!
Problem is that these all happen at different layers, and simple-schema and collection2 can’t tell what the result of the query will be before it does it’s defaultValue / autoValue processing, so even if the upsert bug didn’t exist, it wouldn’t end up receiving the defaultValue and then mongo would have to deal with it.
So I think you’ll need to move the logic you want into user code, check if a document already exists and decide if you’re going to update or insert yourself
Okay I was curious what mongo does with an upsert with a range query and and $inc modifier, and it doesn’t set anything for the query key createdAt and sets the $inc keys with the values in the $inc