question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to set MongoDB optional fields to `undefined`

See original GitHub issue

Problem

Currently, optional fields in MongoDB have the type T | null, while sometimes we want to have really optional field, so it should be T | undefined. It would be nice to have some mechanism to distinguish between null and undefined

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dpetrickcommented, Mar 28, 2022

I personally thought about not actually storing undefined, but removing property when undefined is explicitly passed

undefined is also not really a value (with my non-JS perspective), it’s a property of the data (being absent), so storing it doesn’t make sense. We also talked to Mongo and they’re pushing hard for undefined not being a value / type, so that’s aligned with them.

So yea, anyway, we introduced unset for optional fields in 3.11.1, which achieves the main part of this request: The ability to remove values from documents.

The remaining issue is telling null and undefined apart somehow (e.g. when fetching fields that don’t exist on the document, we currently return null) and unfortunately this is much harder and likely requires a lot of API work. However, I do not consider this part of this issue, which is why I consider this issue closed.

To summarize: You can now unset composite and scalar fields in update operations. If you do not specify a field in a create operation, then it will not be null, but unset (undefined, missing, whatever you want to call it) on the document.

We will follow up soon with another ticket about better distinguishing null and undefined.

0reactions
dpetrickcommented, Mar 28, 2022

FYI follow-up for telling null and undefined apart: https://github.com/prisma/prisma/issues/12555

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting a value on an undefined/null sub-document ...
I'm able to create models from the schema with or without setting the supervisor field. If the value is set at creation, then...
Read more >
After I manually change a field name, the console tells me ...
After I manually changed a field name in mongoDb, my crud operations no longer work. I have read that in this case the...
Read more >
Failed schema validation after insert with sdk's becuase of ...
Goal: Trying to update a single field for a list of object ids All Schema: all fields are optional Object was created with...
Read more >
Failed schema validation after insert with sdk's becuase of ...
hi, After I insert a new Realm-Object via SDK (Swift or Java) that contains a null value I run into a schema validation...
Read more >
Query for Null or Missing Fields
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples. Different query operators in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found