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

feat(model): add insertOne() function to insert a single doc #15162

Merged
merged 4 commits into from
Jan 8, 2025

Conversation

vkarpov15
Copy link
Collaborator

Summary

Fix #14843

insertOne() is similar to create(), but only supports creating a single document. insertOne() is useful for 2 reasons:

  1. Consistency with the MongoDB CRUD API. Mongoose models currently support all of the MongoDB CRUD API methods except insertOne(), which is a bit of a gotcha.
  2. create() with options has proven to be confusing for users, because create(doc, { session }) attempts to create 2 documents, the 2nd with a session property. I don't want to drop support for create(doc1, doc2) (spread args) because that would just create the opposite hard-to-diagnose issue where doc2 gets treated as options. insertOne() offers a neat alternative for users who just want to create 1 doc within a transaction without new.

Examples

@vkarpov15 vkarpov15 added this to the 8.9.4 milestone Jan 6, 2025
@vkarpov15 vkarpov15 modified the milestones: 8.9.4, 8.10 Jan 6, 2025
lib/model.js Outdated Show resolved Hide resolved
lib/model.js Outdated Show resolved Hide resolved
types/models.d.ts Show resolved Hide resolved
@vkarpov15 vkarpov15 merged commit 12e8431 into 8.10 Jan 8, 2025
72 of 79 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-14843 branch January 8, 2025 19:59
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.

2 participants