我正在通过使用Typescript从MongoDB获取默认的MongoDB对象ID。如何获取自定义ID而不是默认的MongoDB对象ID?
{ “ _id”:“ 5bbe053ab10bdf08964443d5 ”, “ title”:“ Moto Z”, “ manufacture_details”:{ “品牌”:“摩托罗拉”, “ model_number”:“ XT1650” }
这是我的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,
},
});
答案 0 :(得分:0)
我用新字段_id更新了Model.ts,它将覆盖默认的对象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,
},
});
现在我可以获取自定义对象ID
{“ _id”:“ M01”,“ title”:“ Moto Z”,“ manufacture_details”:{“ brand”: “ Motorola”,“ model_number”:“ XT1650”}}