-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Model.create(doc, fn) should save all valid documents. #1731
Comments
yeap, I have the same problem. |
Halleluja! Yes, this exactly! |
Same here too. |
I have the same problem, but I vote for two APIs, one where it saves all or nothing, and one for all valid docs like requested above :) |
Having the same problem here! |
@vkarpov15 The following script shows that an error is generated and returned for the first duplicated doc, the rest of the duplicated ones fail silently and all unique docs are created. 7097.js#!/usr/bin/env node
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('useCreateIndex', true);
const { Schema, connection} = mongoose;
const DB = '7097';
const URI = `mongodb://localhost:27017/${DB}`;
const OPTS = { family: 4, useNewUrlParser: true };
const schema = new Schema({
name: {
type: String,
unique: true
}
}, { autoIndex: false });
const tests = '122344445667889'.split('').map(n => { return { name: `test${n}`};});
const compare = '123456789'.split('').map(d => `test${d}`);
const errors = [];
function error(e) {
console.dir(e);
errors.push(e.message);
}
async function run() {
assert.strictEqual(mongoose.version, '5.3.1');
await mongoose.connect(URI, OPTS);
await connection.dropDatabase();
const Test = mongoose.model('test', schema);
await Test.createIndexes();
await Test.create(tests).catch(error);
let created = await Test.find({}).lean();
assert.strictEqual(created.length, 9);
assert.deepStrictEqual(created.map(d => d.name), compare);
assert.strictEqual(errors.length, 1);
assert.ok(/^E11000/.test(errors[0]));
console.log('All Assertions Pass');
await connection.close();
}
run().catch(e => { console.error(e); return connection.close();}); Output:
|
@lineus we could just add a list of saved docs and a list of docs that failed to save in the error. Thoughts? |
sounds good to me. I'll take a stab at it. |
I'm trying to save a set of documents using
Model.create(documents, function(error){...})
I found when a document is invalid, the previous documents were saved but the following will never be saved.I think Model.create should save all valid documents. I would like an API like this.
I would like to do a pull request, but first I want to know what do you think.
The text was updated successfully, but these errors were encountered: