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

Possibility to run create() sequentially #4038

Closed
dciccale opened this issue Apr 3, 2016 · 2 comments · Fixed by #13472
Closed

Possibility to run create() sequentially #4038

dciccale opened this issue Apr 3, 2016 · 2 comments · Fixed by #13472
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@dciccale
Copy link
Contributor

dciccale commented Apr 3, 2016

as @vkarpov15 commented here #723 (comment) the create() method runs a bunch of save() calls in parallel.

It would be useful to implement some kind of callback or sequential promises to allow keeping the order for example in a number sequence like in the following example:

OrderSchema.pre('save', function (next) {
  if (!this.isNew) {
    return next();
  }

  CounterModel.findOneAndUpdate({_id: 'orders'}, {$inc: {n: 1}}, {'new': true, upsert: true}, (err, doc) => {
    if (err) {
      next(err);
    } else {
      this.reference = doc.n;
      next();
    }
  });
});

Output:

[ { reference: '13' },
  { reference: '21' },
  { reference: '16' },
  { reference: '5' },
  { reference: '9' },
  { reference: '19' },
  { reference: '2' },
  { reference: '10' },
  { reference: '11' },
  { reference: '20' },
  { reference: '8' },
  { reference: '18' },
  { reference: '3' },
  { reference: '15' },
  { reference: '7' },
  { reference: '14' },
  { reference: '4' },
  { reference: '17' },
  { reference: '22' },
  { reference: '6' },
  { reference: '1' },
  { reference: '12' } ]

My current use case, is when a user imports a file into my app, for creating a bunch of documents, the reference number goes crazy, and I would expect them to keep the order.

I know this could be easily implemented by iterating myself and running save() however It could be an option in the create() method and probably more performant 👍

@vkarpov15 vkarpov15 added this to the 4.6 milestone Apr 3, 2016
@velichkov
Copy link

👍

@vkarpov15
Copy link
Collaborator

As a workaround you can do async.series() or just .save() in a for loop if you're using co

@vkarpov15 vkarpov15 modified the milestones: 4.10, 4.8 Dec 22, 2016
@sobafuchs sobafuchs added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jan 9, 2017
@vkarpov15 vkarpov15 modified the milestones: 4.13, 4.12 Aug 28, 2017
@vkarpov15 vkarpov15 modified the milestones: 7.x Unprioritized, 7.3.0 Jun 2, 2023
@IslandRhythms IslandRhythms linked a pull request Jun 2, 2023 that will close this issue
vkarpov15 added a commit that referenced this issue Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants