Joel K Thomas
Joel K Thomas

Reputation: 258

TypeScript- Custom Object ID instead of Default MongoDB Object ID

I am getting default MongoDB Object ID from MongoDB by using Typescript. How can I get Custom ID instead of default MongoDB Object ID ?

{ "_id": "5bbe053ab10bdf08964443d5", "title": " Moto Z", "manufacture_details": { "brand": "Motorola", "model_number": "XT1650" } }

This is my Model.ts

import * as mongoose from 'mongoose';

const Schema = mongoose.Schema;

export const ProductSchema = new Schema({
    title: {
        type: String,
    },
    manufacture_details: {
        brand: String,
        model_number: String,
      },
});

Upvotes: 1

Views: 865

Answers (1)

Joel K Thomas
Joel K Thomas

Reputation: 258

I Updated the Model.ts with a new field _id which will be overwriting the default Object ID

import * as mongoose from 'mongoose';

const Schema = mongoose.Schema;

export const ProductSchema = new Schema({
    _id: {
        type: String,
    },
    title: {
        type: String,
    },
    manufacture_details: {
        brand: String,
        model_number: String,
      },
});

Now i am able to get Custom Object ID

{ "_id": "M01", "title": " Moto Z", "manufacture_details": { "brand": "Motorola", "model_number": "XT1650" } }

Upvotes: 1

Related Questions