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

docs(populate): add a little more info on why we recommend using ObjectId for _id #13474

Merged
merged 1 commit into from Jun 7, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13400

Summary

Re: #13400, populate docs don't really explain why we recommend using ObjectId for _id and ref. Added a note about that, and moved the note to the "Saving Refs" section.

Examples

You can set the `ref` option on `ObjectId`, `Number`, `String`, and `Buffer` paths.
`populate()` works with ObjectIds, numbers, strings, and buffers.
However, we recommend using ObjectIds as `_id` properties (and thus ObjectIds for `ref` properties) unless you have a good reason not to.
That is because MongoDB will set `_id` to an ObjectId if you create a new document without an `_id` property, so if you make your `_id` property a Number, you need to be extra careful not to insert a document without a numeric `_id`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe mention that mongoose can help with that via validators, like using required: true and for non-default types to have to use default: () => newType to generate a new id (via a function instead of statically)

@BioSurienDG
Copy link

@vkarpov15 I just had a question about this PR. Should we be saying we are using the mongodb ObjectId type defn rather than our own b/c I found that we have defined in our type defns to use the mongoose specific one that extends the mongodb one but that isn't the case in reality (from testing). See my issue here: #13473

@vkarpov15
Copy link
Collaborator Author

@BioSurienDG I'm not sure what your question has to do with this PR? I'd generally recommend using mongoose.Types.ObjectId in TypeScript; but it doesn't really matter unless you're using the _id getter.

@vkarpov15 vkarpov15 merged commit 723f040 into master Jun 7, 2023
1 check passed
@vkarpov15 vkarpov15 added this to the 7.2.3 milestone Jun 7, 2023
@hasezoey hasezoey deleted the vkarpov15/gh-13400 branch June 7, 2023 09:25
@BioSurienDG
Copy link

BioSurienDG commented Jun 28, 2023

@BioSurienDG I'm not sure what your question has to do with this PR? I'd generally recommend using mongoose.Types.ObjectId in TypeScript; but it doesn't really matter unless you're using the _id getter.

Ya no worries. I had thought it was a documentation issue that could be resolved in this but was actually a bug - pls disregard my comment on the PR 🙂

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

Successfully merging this pull request may close these issues.

Ref type docs missing
4 participants