Skip to content

Commit

Permalink
docs: rework several code examples that still use callbacks
Browse files Browse the repository at this point in the history
Fix #13616
  • Loading branch information
vkarpov15 committed Jul 19, 2023
1 parent 587983e commit f018571
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
24 changes: 7 additions & 17 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ const schema = new mongoose.Schema({
});
const Model = db.model('Test', schema);

Model.create([{ name: 'Val' }, { name: 'Val' }], function(err) {
console.log(err); // No error, unless index was already built
});
// No error, unless index was already built
await Model.create([{ name: 'Val' }, { name: 'Val' }]);
```

However, if you wait for the index to build using the `Model.on('index')` event, attempts to save duplicates will correctly error.
Expand All @@ -92,21 +91,12 @@ const schema = new mongoose.Schema({
});
const Model = db.model('Test', schema);

Model.on('index', function(err) { // <-- Wait for model's indexes to finish
assert.ifError(err);
Model.create([{ name: 'Val' }, { name: 'Val' }], function(err) {
console.log(err);
});
});

// Promise based alternative. `init()` returns a promise that resolves
// when the indexes have finished building successfully. The `init()`
// Wait for model's indexes to finish. The `init()`
// function is idempotent, so don't worry about triggering an index rebuild.
Model.init().then(function() {
Model.create([{ name: 'Val' }, { name: 'Val' }], function(err) {
console.log(err);
});
});
await Model.init();

// Throws a duplicate key error
await Model.create([{ name: 'Val' }, { name: 'Val' }]);
```

MongoDB persists indexes, so you only need to rebuild indexes if you're starting
Expand Down
9 changes: 4 additions & 5 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,10 @@ schema.post('update', function(error, res, next) {
});

const people = [{ name: 'Axl Rose' }, { name: 'Slash' }];
Person.create(people, function(error) {
Person.update({ name: 'Slash' }, { $set: { name: 'Axl Rose' } }, function(error) {
// `error.message` will be "There was a duplicate key error"
});
});
await Person.create(people); function(error) {

// Throws "There was a duplicate key error"
await Person.update({ name: 'Slash' }, { $set: { name: 'Axl Rose' } });
```
Error handling middleware can transform an error, but it can't remove the
Expand Down
6 changes: 4 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3045,8 +3045,10 @@ Model.startSession = function() {
*
* #### Example:
*
* const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
* Movies.insertMany(arr, function(error, docs) {});
* await Movies.insertMany([
* { name: 'Star Wars' },
* { name: 'The Empire Strikes Back' }
* ]);
*
* @param {Array|Object|*} doc(s)
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/4.9/classes/Collection.html#insertMany)
Expand Down
9 changes: 3 additions & 6 deletions test/model.insertMany.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ describe('insertMany()', function() {
const Movie = db.model('Movie', schema);
const start = Date.now();

const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
return Movie.insertMany(arr).
return Movie.insertMany([{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }]).
then(docs => {
assert.equal(docs.length, 2);
assert.ok(!docs[0].isNew);
Expand Down Expand Up @@ -93,8 +92,7 @@ describe('insertMany()', function() {
}, { timestamps: true });
const Movie = db.model('Movie', schema);

const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
let docs = await Movie.insertMany(arr);
let docs = await Movie.insertMany([{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }]);
assert.equal(docs.length, 2);
assert.ok(!docs[0].isNew);
assert.ok(!docs[1].isNew);
Expand Down Expand Up @@ -299,8 +297,7 @@ describe('insertMany()', function() {
});
const Movie = db.model('Movie', schema);

const arr = [{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }];
let docs = await Movie.insertMany(arr);
let docs = await Movie.insertMany([{ name: 'Star Wars' }, { name: 'The Empire Strikes Back' }]);
assert.equal(docs.length, 2);
assert.equal(calledPre, 2);
assert.equal(calledPost, 1);
Expand Down

0 comments on commit f018571

Please sign in to comment.