skip to Main Content

I am getting different ID value in JSON response when checking variable as id & _id in DTO

CASE I ( using id as variable )

DTO for the response

export class UserDetail {
    
        @Expose( )
        id: string; //HERE ID is used which gives original id as in db
    
        @Expose( )
        name: string;
    
        @IsOptional( )
        @Expose( )
        mobile: string;
   }

Response for this is:

        {
            "id": "6229df5cc32d3aaef32525e0", // Correct id as in db
            "name": "Name1 Title1",
            "mobile": "AB391C339",
        },
        {
            "id": "6229df7bc32d3aaef32525e3", // Correct id as in db
            "name": "Name2 Title2",
            "mobile": "CDE393F339",
        }

CASE II ( using _id as variable )

Now changing DTO

export class UserDetail {
        
       @Expose( )
       _id: string; //HERE _id is used which gives different id value from original one and subsequent ids are in incremental fashion
        
       @Expose( )
       name: string;
        
       @IsOptional( )
       @Expose( )
       mobile: string;
  }

Response for this is:

        {
            "_id": "624023c8193f2404f8312ccb", // Not same as present in db
            "name": "Name1 Title1",
            "mobile": "AB391C339"
        },
        {
            "_id": "624023c8193f2404f8312ccc", // Not same as present in db
            "name": "Name2 Title2",
            "mobile": "CDE393F339"
        },

Schema for user is:

@Schema( )
export class User extends Document {

    @Prop( { required: true } )
    name: string;

    @Prop( { required: true } )
    mobile: string;
}

Another Observation: With the use of _id in dto, all the ids in response are in incremental fashion. eg. cb, cc, cd, ce
& with only id in dto it’s showing ids as original id in db.

Library & Framework Used: NestJs, Fastify, Mongoose, Class-Transformer

3

Answers


  1. i don’t quite understand your question…

    you need to give some information about what libraries you are using and what you are actually trying to do.

    so far i can only tell you that mongodb uses the _id filed as a unique identifier for each record.

    the value for this identifier also relies on the time at which it is created (read more)

    some libraries also add a virtual field id to each entry which links back to _id, this is only done for cosmetic purposes.

    each table entry needs such a unique id for indexing and relational data modeling.

    i hope this provided at least something useful to you

    Login or Signup to reply.
  2. According to what you wrote only explanation that came to my mind is that your documents accually contain both fields id and _id, that might happen if someone imported documents directly to database in some wired way. It happened to me once when I imported raw json backup.

    Login or Signup to reply.
  3. As far as I can see from the code you shared, there is no such thing as "id" in you schema.

    Look at this post and see if it helps you figure it out.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search