Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectID(undefined) after update #90

Open
a-maggioni opened this issue Jan 3, 2019 · 1 comment
Open

ObjectID(undefined) after update #90

a-maggioni opened this issue Jan 3, 2019 · 1 comment

Comments

@a-maggioni
Copy link

a-maggioni commented Jan 3, 2019

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

@angelo-hub
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants