Does calling the property .id automatically convert _id to a string format?

Oscar

Currently have something like this in my mongoose application:

const user = await User.findById(request.params.id)

Whenever I console.log(user), I get the entire document object, alongside the _id: new ObjectId("idstring") property. There is no reference to id but rather to just _id.

However when I console.log(user.id) I get the returned _id in string format.

How is this possible? Do mongoose models automatically convert the objectID to a string when you call "id" to the object?

Apoorva Chikara

Mongoose assigns each of your schemas an id virtual getter by default which returns the documents _id field cast to a string, or in the case of ObjectIds, its hexString.

Because of the virtuals, you are getting the results. So, id is a getter that gives the string's representation of the field _id.

Check here for documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Property id does not exist on type string

laravel collection/format printing |Property [id] does not exist on this collection instance

Cast(convert) id to string

Grails - include id property when calling .properties

Why does calling a property on a string not give an error?

Convert ID from query to String

Does expressjs automatically generate a new session id?

Does postgres automatically generate an id for every row?

Linking to controller action using Twig's path() does not automatically find the Entity for the given id, string given

how to convert a tag id into a polymer property?

"Property 'id' does not exist on type 'Promise<FirmDoc>'" // "Type '() => Promise<string[]>' is not assignable to type '() => string[]'."

Convert ID number into date format in oracle sql

Convert Thread Process Id to Hex format

Property [id] does not exist on this collection instance

Property 'id' does not exist on type 'object'?

Property 'id' does not exist on type 'typeof Foods'

Property id does not exist on type 'never'

Property [id] does not exist on the Eloquent builder instance

Property [id] does not exist in Laravel 5.5

Property 'id' does not exist on type 'any[]'

Property 'id' does not exist on type 'never'. typescript

Property 'id' does not exist on type of function argument

Property [id] does not exist on this collection instance?

Property 'id' does not exist on type 'Order[]'

nHibernate generate Guid for Id but property as string

TypeError: Cannot create property '_id' on string

TypeError: Cannot create property 'next' on string '/:id'

No such property: id for class: java.lang.String

ValueError: could not convert string to float: id