Mongoose findByIdAndUpdate() updates the wrong entry

I'm calling the following Mongoose function:

User.findByIdAndUpdate(
  '76561198015207414', 
  {}, 
  { upsert: true }
);

My user collection is empty before calling this. After running the function, my user collection adds this document:

{
    "_id": 76561198015207410
}

Why would it be updating the wrong entry in the collection?

Answers


It's likely that the _id that you've defined for User is a Number:

var UserSchema = Schema({
    _id : Number,
    /* more */
});

If you use JavaScript and convert the String passed to the call to findByIdAndUpdate and convert it to a Number, you'll see the precision isn't retained:

> var x=76561198015207414
> x
76561198015207410

You'll see the variable x contains the _id you've shown in your question.

You'll need to either switch to Strings or be sure that the numbers do not exceed a Number.


Need Your Help

iPhone - textField in a tableViewCell - Scrolling off screen clears the value

iphone uitableview uitextfield

I have a textField inside a tableViewCell. I recently noticed that if I enter a value in the textBox, and then scroll the cell off the screen, the value in the textbox disappears.

Adding links to this PHP statement

php javascript wordpress plugins hyperlink

Here is a PHP statement that basically sets off a javascript image slider. Only problem is I can't figure out how to get each individual image to a link to a different page. Any tips or suggestio...