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

Auto increment id don't work with Model.insertMany #77

Open
phong-et opened this issue Oct 11, 2019 · 3 comments
Open

Auto increment id don't work with Model.insertMany #77

phong-et opened this issue Oct 11, 2019 · 3 comments

Comments

@phong-et
Copy link

phong-et commented Oct 11, 2019

Hi guys, as title, has anyone got this issue like me ?

I have a productSchema and plugged autoIncrement

const productSchema = new Schema({
    _id: Number,
    // ...
})
productSchema.plugin(autoIncrement, {inc_field:'_id'})
const Product = db.model('product', productSchema, 'product_new');

Use .save it works well, _Id field value is a Number

jsonProduct = {...}
let product = new Product(jsonProduct)
    product.save(function (err, product) {
      if (err) return console.error(err);
      db.connection.close()
});

Use .insertMany, _id field is ObjectID not a Number

jsonProducts = [{...},{...}...]
Product.insertMany(jsonProducts, function (err, product) {
      if (err) return console.error(err);
      db.connection.close()
});

@dandv
Copy link
Contributor

dandv commented Apr 15, 2020

I have the same problem as in #7, but the id field wasn't created at all. The code is in #83. Model Foo uses .save() and the fooId field gets created. Model Bar uses .insertMany() and the barId field doesn't get created at all. (I tried new Bar(...).save() and that did create the barIds correctly.)

@ramiel, is it possible that #7 has reappeared? Or was it not possible to fix that issue and the workaround is to use the slow .create()?

@sajadghawami
Copy link

Any fix on this?

@ramiel
Copy link
Owner

ramiel commented Jul 2, 2020

So, as specified on #7 this cannot be solved. insertMany doesn't call the necessary middlewres and so using several create is the way to go

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

No branches or pull requests

4 participants