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 valueOf not working #10741

Closed
m-weeks opened this issue Sep 16, 2021 · 3 comments
Closed

ObjectId valueOf not working #10741

m-weeks opened this issue Sep 16, 2021 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@m-weeks
Copy link

m-weeks commented Sep 16, 2021

Do you want to request a feature or report a bug?
bug

What is the current behavior?
checking equality of two ObjectIds with the same ID is always false.

When I run myObjectId.valueOf() it appears to output a new ObjectId instead of the string representation like expected.

If the current behavior is a bug, please provide the steps to reproduce.

var mongoose = require('mongoose');
var ObjectId = mongoose.Types.ObjectId;

var a = new ObjectId('6143b55ac9a762738b15d4f0');
var b = new ObjectId('6143b55ac9a762738b15d4f0');

console.log(a == b); // false
console.log(a.valueOf() == b.valueOf()); // false

I believe the issue is due to this check:

if (ObjectId.prototype.valueOf === void 0) {
  ObjectId.prototype.valueOf = function objectIdValueOf() {
    return this.toString();
  };
}

When I run the ObjectId.prototype.valueOf === void 0 on the mongodb driver ObjectId it's false, so I believe the overridden function isn't being applied.

What is the expected behavior?
According to the 6.0 Migration document:

ObjectId valueOf()
Mongoose now adds a valueOf() function to ObjectIds. This means you can now use == to compare two ObjectId instances.

Although this doesn't seem to be the case.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 12.16
Mongoose: 6.0.6
MongoDb: 4.4

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 17, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

const a = ObjectId('6143b55ac9a762738b15d4f0');
const b = ObjectId('6143b55ac9a762738b15d4f0');

console.log(a == b);
console.log(a.valueOf() == b.valueOf());

@vkarpov15 vkarpov15 modified the milestones: 6.0.9, 6.0.8 Sep 18, 2021
@vkarpov15
Copy link
Collaborator

Re: valueOf() returning an object, that's a bug and we'll fix it for v6.0.8.

Re: the claim that "you can now use == to compare two ObjectId instances", that's unfortunately also wrong. Two objects cannot be == each other without being the same reference. Equality in JavaScript is pretty strict about that. But what you should be able to do now is compare an object id against a string:

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

const a = ObjectId('6143b55ac9a762738b15d4f0');

a == '6143b55ac9a762738b15d4f0'; // true

@mohd-akram
Copy link
Contributor

It seems mongodb's ObjectId already supports this, and it also has an .equals method for comparing two ids, so it seems overriding valueOf is not necessary.

const a = new require('mongodb').ObjectId();
console.log(a == a.toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

4 participants