Reputation: 127
For whatever reason, mongoose seems to be unable to actually locate my document after a find
call because of a custom ID.
The source of the problem:
// ...
models.Item.findOne().where({ _id: item.id }).exec((i) => {
console.log(item.id, i);
if (!i) i = new models.Item({ _id: item.id, href: item.href, name: item.name });
// ...
The console just logs the following:
> 0 null
> MongoError: E110000 duplicate key error index: items.$_id_ dup key: { : 0 }
Indicating it seems to be unable to locate the document with the ID zero, but immediately follows up by complaining the key already exists with a value of 0 when I try to create a new one.
Upon looking at the database, I can clearly see my document with an _id
of 0.
{
"id": 0,
// ...
}
And this is the schema I'm using:
const ItemSchema = new mongoose.Schema({
_id: { type: Number, min: 0, max: 400000, unique: true, required: true },
// ...
}
I've tried find
and findOne
in the chained and un-chained versions and findById
. None of them are yeilding the existing document. Why?
Upvotes: 0
Views: 1414
Reputation: 312149
You're missing the error parameter in your exec
callback. It should be:
models.Item.findOne().where({ _id: item.id }).exec((err, i) => {...
The second parameter is the document result.
Upvotes: 4