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

.id returns string while ._id returns object. is this a bug? #548

Closed
adamz123 opened this issue Sep 30, 2011 · 6 comments
Closed

.id returns string while ._id returns object. is this a bug? #548

adamz123 opened this issue Sep 30, 2011 · 6 comments

Comments

@adamz123
Copy link

In the following function, test1 is a string because the virtual path runs through a different codeset and runs the toString() function, whereas test2 is an object with the 'id' set to the hex id. The console.log will produce the same result since console.log will call the .toString function in the object. However, I was under the impression .id and ._id were supposed to be the same thing. Which one am I supposed to be using or is this a bug?

function createSegment(options, callback){
var location = options.location;
var newSegment = new Segment({coordinates: new Array(location)});
newSegment.save(function(err){
if(err) {callback(err); return;}
debugger;
var test1 = newSegment.id;
var test2 = newSegment._id;
console.log(newSegment.id);
console.log(newSegment._id);
callback(null, newSegment.id);
});
}

@donpark
Copy link

donpark commented Sep 30, 2011

_id is an instance of ObjectId which is native-mongodb-driver's representation of binary structure MongoDB uses.

id is convenience method for retrieving string representation of _id.

What I'm wondering is whether id really got added to Mongoose since it might conflict with my own impl. of id.

@aheckmann
Copy link
Collaborator

yes @donpark is correct. ObjectIds contain the raw MongoDB binary and don't work with templating so we provide the id convenience method to convert them to hexstrings. Mongoose also transparently casts these hexstrings back to ObjectIds for you when querying / creating objects etc.

This is the flow we are making simpler:

MongoDB -> ObjectId -> cast toHexString -> form -> submit -> cast from hexString -> ObjectId -> MongoDB

@c4milo
Copy link

c4milo commented Sep 30, 2011

wait, does it mean that we can't use a property "id" in our Schemas? I'm using my own "id" implementation as well

@aheckmann
Copy link
Collaborator

@c4milo you can use your own id. We only add it if you haven't.

@c4milo
Copy link

c4milo commented Oct 1, 2011

nice, tks

@technotronic12
Copy link

technotronic12 commented Jun 11, 2017

@aheckmann I am also using my own id (a string guid). recently I started receiving this error: Named property 'id' of types 'IMaintenance' and 'Document' are not identical. Any idea what might cause it? Is it related to the current issue?

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

5 participants