Skip to content

Support for options argument when creating a single document in Model.create(): use Model.insertOne() instead #14843

Closed
@kmarchenko

Description

@kmarchenko

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Enhance the Model.create() method to allow passing an options argument when creating a single document. Currently, this is only possible when documents are passed as an array, which is inconvenient for cases where only a single document needs to be created.

Motivation

The current implementation of the Model.create() method requires documents to be passed as an array when using the options argument, even if only a single document is being created. This approach is less intuitive and can lead to unnecessary complexity, such as having to handle an array in return when only a single document is expected. Allowing the options argument to be used with a single document directly would result in cleaner, more straightforward code and avoid the need to deal with array structures when they aren't necessary.

Example

Current Implementation:

const [document] = await model.create(
  [
    {
      name: 'example',
    },
  ],
  {
    session: ClientSession,
  },
);

Proposed Implementation:

const document = await model.create(
  {
    name: 'example',
  },
  {
    session: ClientSession,
  },
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new featurenew featureThis change adds new functionality, like a new method or class

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions