Ability to set MongoDB optional fields to `undefined`
See original GitHub issueProblem
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:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 forundefined
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
andundefined
apart somehow (e.g. when fetching fields that don’t exist on the document, we currently returnnull
) 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
andundefined
.FYI follow-up for telling
null
andundefined
apart: https://github.com/prisma/prisma/issues/12555