Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Number passed into an update changes when saved into a collection (MongoDB)
    primarykey
    data
    text
    <p>I have a Mongoose schema that uses a numerical value as the <code>_id</code> e.g. <code>102375848308956134094</code>. When I save the collection and <code>findById</code> in Mongoose or <code>find({ _id: 102375848308956134094 });</code> in the Mongo shell the correct document is returned though the <code>_id</code> is different to the one previously provided (instead its <code>102375848308956140000</code>).</p> <p>This is a problem as if I were to save a collection the the <code>_id</code> <code>102375848308956134095</code> (notice the last digit changed from a <code>4</code> to a <code>5</code>), MongoDB errors out (<code>E11000 duplicate key error index... dup key: { : 1.023758483089561e+20</code>).</p> <p>Is MongoDB treating the <code>Number</code> as a floating point number?</p> <p>Here is an example query:</p> <pre><code>&gt; db.users.find({ _id: 102375848308956134094 }).pretty(); { "_id" : 102375848308956140000, "access_token" : "", "access_token_expires" : ISODate(""), "given_name" : "Jonathon", "refresh_token" : "" } </code></pre> <p><em>NB: I have removed the <code>access_token</code> etc. values.</em></p> <p>When that document was saved however, I absolutely specified the <code>_id</code> value to be <code>102375848308956134094</code>.</p> <p>Any ideas what is going on?</p> <p>Here is my schema:</p> <pre><code>/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/ 'use strict'; module.exports = function (mongoose) { var Schema = new mongoose.Schema({ _id: Number, access_token: String, access_token_expires: Date, given_name: String, refresh_token: String }); return mongoose.model('User', Schema); }; </code></pre> <p>And this is the <code>upsert</code>:</p> <pre><code>... models.user.update( { _id: user.id }, { access_token: access_token, access_token_expires: new Date(Date.now() + (59 * 60 * 1000)), given_name: user.given_name, refresh_token: refresh_token }, { upsert: true }, /*TODO: Handle upsert errors*/ function (err, numberAffected, raw) { if (err) { console.log(err); } else { delete req.session.state; req.session._id = user.id; res.redirect('/'); } } ); ... </code></pre> <p>All help, as always, is much appreciated!</p> <p>/<em>Edit</em>/</p> <p>I have tried @JohnnyHK's answer of using <code>Long</code> numbers however, the <code>_id</code> still seems to be stored incorrectly in the document. It is saving the <code>_id</code> as <code>-8304616133301175602</code> instead of <code>102375848308956134094</code>. I have tried several queries to <code>find</code> the document but only only supplying the <code>_id</code> as <code>-8304616133301175602</code> returns the correct document e.g.</p> <pre><code>&gt; db.users.find().pretty(); { "_id" : NumberLong("-8304616133301175602"), "access_token" : "ya29.1.AADtN_VyfAvBlam2HCERpI0JcJkcwg22t1124tZw0G7pgRyTcaIuGU-dX3H4Q-M", "access_token_expires" : ISODate("2013-12-09T12:42:53.098Z"), "given_name" : "Jonathon", "refresh_token" : "1/-2GQ_s3JogCr45Z1CBKWBHTEcjE0Nda9xkpFFdl7wT0" } &gt; db.users.find({ _id: 102375848308956134094 }).pretty(); &gt; db.users.find({ _id: NumberLong(102375848308956134094) }).pretty(); &gt; db.users.find({ _id: NumberLong('102375848308956134094') }).pretty(); Mon Dec 9 11:46:26.433 Error: could not convert "102375848308956134094" to NumberLong &gt; db.users.find({ _id: -8304616133301175602 }).pretty(); { "_id" : NumberLong("-8304616133301175602"), "access_token" : "", "access_token_expires" : ISODate(""), "given_name" : "Jonathon", "refresh_token" : "" } </code></pre> <p>Any thoughts as to why this may be?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload