Skip to content
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

Inserting field with undefined value inserts null instead of undefined #146

Closed
jedborovik opened this issue Jul 13, 2016 · 9 comments
Closed

Comments

@jedborovik
Copy link

For example:

collection.insert({ name: undefined })

inserts { name: {} } and not { name: undefined }. I'm using monk 3.0.4.

The mongo shell inserts { name: undefined } for same insert command.

@mathieudutour
Copy link
Collaborator

it's fixed in monk@3.0.6

@jedborovik
Copy link
Author

jedborovik commented Jul 14, 2016

Just tried with monk@3.0.6 and rather than inserting { name: undefined }, it is inserting { name: null }.

One nice thing about having undefined over null is ES6 default values only act on undefined, not null.

The benefit of { name: undefined } is it allows something like:

collection.findOne({ email: 'me@email.com' }).then(({ name = 'No name' }) => {
  // yadda yadda
});

We can definitely delete undefined fields before inserting, but it would be nice to have some control over null vs undefined.

@mathieudutour
Copy link
Collaborator

Indeed, it would be nice but the difference between undefined and null is not supported in the nodejs mongo driver (http://devblog.me/wtf-mongo part 2) (meteor/meteor#1646 (comment))

I'll think about what we can do but I'm afraid it's not much

@mathieudutour mathieudutour reopened this Jul 14, 2016
@mathieudutour
Copy link
Collaborator

maybe deleting the undefined fields on insert?

@mathieudutour mathieudutour changed the title Inserting field with undefined value inserts empty object not undefined Inserting field with undefined value inserts null instead of undefined Jul 14, 2016
@jedborovik
Copy link
Author

Yikes, I didn't realize the BSON spec deprecated undefined.

Deleting the undefined fields on insert sounds like a great suggestion. That does seem to mimic the behavior of a javascript object more than having a null value.

@mathieudutour
Copy link
Collaborator

This can be implemented as a middleware (see https://automattic.github.io/monk/docs/middlewares.html) in user-land.

Will close this as it probably shouldn't be core

@jaredpalmer
Copy link

@mathieudutour can you show what the middleware would look like?

@mathieudutour
Copy link
Collaborator

hum maybe something like:

module.exports = function (context) {
  return function (next) {
    return function (args, method) {
      if (!args.data) {
        return next(args, method)
      }

      if (typeof args.data !== 'object') {
        return next(args, method)
      }

      const dataWithoutUndefined = Object.keys(args.data).reduce((prev, k) => {
        if (typeof args.data[k] !== 'undefined') {
          prev[k] = args.data[k]
        }
        return prev
      }, {})

      args.data = dataWithoutUndefined
      return next(args, method)
    }
  }
}

@ceoworks
Copy link

it seems like mongo has connection option ignoreUndefined, which could solve this issue.
https://mongodb.github.io/node-mongodb-native/2.1/reference/connecting/connection-settings/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants