mongo-mock icon indicating copy to clipboard operation
mongo-mock copied to clipboard

ObjectID(undefined) after update

Open a-maggioni opened this issue 6 years ago • 1 comments

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).

a-maggioni avatar Jan 03 '19 16:01 a-maggioni

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.

angelo-hub avatar Jan 11 '19 19:01 angelo-hub