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

Bug: insertMany fails on Decimal Types #5190

Closed
alaa-ismail opened this issue Apr 20, 2017 · 5 comments
Closed

Bug: insertMany fails on Decimal Types #5190

alaa-ismail opened this issue Apr 20, 2017 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@alaa-ismail
Copy link

When using insertMany on a model whose schema contains a Decimal type, the operation fails with the error:
Error: key $numberDecimal must not start with '$'

Steps to reproduce:
The following code reproduces the issue:

const mongoose  = require('mongoose');
mongoose.Promise = global.Promise;

mongoose.connect('mongodb://localhost:27017/test');

var schema = new mongoose.Schema({
    amount : mongoose.Schema.Types.Decimal
});
var Money = mongoose.model('Money', schema);

Money.create({amount : '123.45'});  // Works fine

// This does not work
Money.insertMany([{amount : '123.45'},{amount : '321.54'}]).then((docs) => { 
    console.log(docs.length);
}).catch((err) => {
    console.log(err);
});

Current Behavior:
The insert operation fails with the error:
Error: key $numberDecimal must not start with '$'

Expectation:
I expect the documents passed to insertMany to be inserted into MongoDB.

Versions:
Mongoose v4.9.5
MongoDB v3.4.3
Node v6.9.1

@sobafuchs
Copy link
Contributor

thanks for the full repro script, this looks like a bug

@sobafuchs sobafuchs added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 25, 2017
@sobafuchs
Copy link
Contributor

it looks like the mongoose insertMany() debug output might shed some light on this:

Mongoose: money.insertMany([ { __v: 0, amount: { '$numberDecimal': '132.45' }, _id: 58fea3fe3e
4166463e444c43 }, { __v: 0, amount: { '$numberDecimal': '321.54' }, _id: 58fea3fe3e4166463e444
c44 } ])

i believe in mongo it should be setting it as Decimal128 or NumberDecimal and the create() function seems to do just that:

Mongoose: money.insert({ amount: Decimal128 { _bsontype: 'Decimal128', bytes: <Buffer 39 30 00
 00 00 00 00 00 00 00 00 00 00 00 3c 30> }, _id: ObjectId("58fea4ef8a63684695772839"), __v: 0
})

@vkarpov15 vkarpov15 added this to the 4.9.7 milestone Apr 30, 2017
vkarpov15 added a commit that referenced this issue Apr 30, 2017
vkarpov15 added a commit that referenced this issue May 1, 2017
@nicroto
Copy link

nicroto commented Jan 26, 2019

I am getting this same error, when I install my node dependencies with npm install --production.
In development, the same create query works properly. Here is (a simplified ver of) the document I try to insert:

    MyModel.create({
      ...
      "price": {
        "$numberDecimal": "9.99"
      }
      ...
    });

And this is the model descriptor:

{
  ...
  "attributes": {
    ...
    "price": {
      "type": "decimal"
    },
    ...
  }
}

(strapi creates this schema for me, so I don't know if it is directly compatible with mongoose description of a model, or it's a layer of abstraction on top of it)

mongoose seems to be latest (v5.4.6). (it's myProj -> dep -> dep -> mongoose, so at least that's fortunate in this situation)

MongoDB: I've tested only with 3.4.1 (official kubernetes helm chart, I am not sure on what OS base image is the bitnami/mongodb 3.4.1 building on) and 3.6.3 (os x 10.13.6) and the issue reproduced in both places.

@vkarpov15
Copy link
Collaborator

@nicroto can you provide code samples for how you're using strapi to generate this?

Also, you're not experiencing the exact same issue, Model.insertMany() is different from Model.create()

@nicroto
Copy link

nicroto commented Jan 29, 2019

Thanks for the reply @vkarpov15.

It was strapi's fault. They were setting the type to float instead of decimal and the create wasn't able to parse the value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants