New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore undefined fields when inserting/updating in Mongo #9444
Conversation
The Mongo Node driver that Meteor uses currently replaces `undefined` field values with `null`, when doing an insert/update. This approach can lead to unexpected behaviour, as outlined in meteor#1646, meteor#6051 and several other issues. This commit configures the default Mongo connection to `ignoreUndefined` fields, which means `undefined` fields are not inserted/updated, instead of being inserted/updated as `null`. Fixes meteor#6051.
It wasn't, for such a breaking change, updating the patch version is not enough. Do you imagine with : find({ userId }) -> from no one to everyone :-( It takes time to find such a bug, because it does not throw... |
@romainkoenig Yes, this is a painful issue to resolve. It's a bug either way; either you end up with unexpected results when you have your Just to sum up though - the changes made here were intended to help address an existing bug caused by Meteor's automatic assumption that |
This has caused a major issue in one of our apps. At first we thought it was only under quite specific circumstances but actually it's across the board. I understand the motivation for the change in design, makes sense, but I'm basically speechless that even after @romainkoenig mentioned here that it's a really major change and really should be documented more clearly in the changelog, he was just ignored? |
Meteor's current behaviour when handling Mongo inserts/updates with
undefined
fields differs when coming from the client versus the server:Client:
leads to the following in Mongo
Server:
leads to the following in Mongo
As discussed in issue #6051, the approach taken for queries initiated from the client or server should be consistent.
This is a long standing issue that is caused by a few things:
undefined
is not a valid JSON/EJSON type so when client side Mongo inserts/updates are sent to the server for handling, theundefined
fields are stripped.undefined
as deprecated, which means the use ofundefined
within the Mongo landscape is not recommended. This has lead to some difficulties when usingundefined
in Mongo queries as howundefined
should be handled has been interpreted in different ways by different Mongo client authors. By default the official Mongo Node client that Meteor uses convertsundefined
tonull
when inserting/updating.There has been a lot of discussion on the interwebs around how the Mongo Node driver handles
undefined
fields, and the problems that can arise from just convertingundefined
tonull
. To that end, the Mongo Node driver team added support for aignoreUndefined
connection option a while back (which isfalse
by default for backwards compatibility reasons). WhenignoreUndefined
istrue
, fields withundefined
values are dropped when inserting/updating, instead of being converted tonull
.This PR sets the
ignoreUndefined
option to true for all Mongo connections, which makes the handling of client/server initiated Mongo inserts/updates consistent.This could potentially be a backwards incompatible change if an app is say expecting an
undefined
field to be inserted as anull
field. This change does better align with Mongo's recommendation of not usingundefined
however, so although some backwards compatibility might be sacrificed, that sacrifice is essential to better align with Mongo. Mentioning this in theHistory.md
should hopefully be enough.Fixes #6051.