mongo-mock
mongo-mock copied to clipboard
ObjectID(undefined) after update
When you update a document, all the fields with type equal to ObjectID get messed up (except for the "_id" field).
Sample code
const mongodb = require('mongo-mock');
const ObjectID = require('mongo-mock').ObjectID;
async function main() {
const db = await mongodb.MongoClient.connect('mongodb://fake.db:1111/db');
const collection = db.collection('collection');
const _id = new ObjectID();
const otherId = new ObjectID();
const obj = { _id, otherId, name: 'insert'};
await collection.insertOne(obj);
const res1 = await collection.findOne({ _id });
await collection.updateOne({ _id }, { $set: { name: 'update' } });
const res2 = await collection.findOne({ _id });
console.log(res1.otherId);
console.log(res2.otherId);
}
main().then(() => { process.exit() });
Expected result res1.otherId should be equal to res2.otherId.
Actual result res2.otherId is equal to ObjectID(undefined).
You're right the mock runs under the assumption that the only objectID is the _id
, will fix the merge/update code to check for object type rather than just plucking _id
by default.