1

In some situation, I need to store number such as Infinity and -Infinity into MongoDB using Mongoose. I have try in MongoDB shell, it works. But When I come to Mongoose, it fails. Any suggestion? Or this won't work at all?

In addition, it would be event better if I can insert NaN value into collections, is it possible?

ADD:

I simplify the code to as below:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

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

var ResultSchema = new Schema({
  account: {
    type: Number,
    required: true,
    index: true,
    unique: true
  },
  profitLostRatio: Number,
});


var Result = mongoose.model('Result', ResultSchema);

var result = new Result({
  account: 18790314,
  profitLostRatio: Infinity
});

result.save(function(err, result) {
  if (err) {
    console.log(err);
  } else {
    console.log(JSON.stringify(result));
  }
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Ryan
  • 800
  • 1
  • 13
  • 27
  • What's your code? What's your data type for model field? – michelem Jun 26 '15 at 09:54
  • The result is as below: {"__v":0,"account":18790314,"profitLostRatio":null,"_id":"558d253eeb187596351a3335"} The profitLostRatio become null – Ryan Jun 26 '15 at 10:11
  • 2
    Could the problem be that the database saves it correctly but JSON.stringify can not deal with Infinity or NaN values? [According to the JSON specification](http://json.org/) these values can not be expressed in JSON. – Philipp Jun 26 '15 at 10:20
  • Thank you. You are correct ! In mongodb shell, it is already Infinity. Then how could I represent Infinity in JSON, or do I need to specially treat these values before sending these values in JSON and after received these values? – Ryan Jun 26 '15 at 10:55

0 Answers0