【问题标题】:using simple id not ObjectId with mongodb在 mongodb 中使用简单的 id 而不是 ObjectId
【发布时间】:2020-06-08 11:52:38
【问题描述】:

我想在客户端(角度)内的 bdd mongodb 中存储新集合我有一个表格要填写,当我点击提交时,一个新集合会像这样保存在 mongodb 中:

{
    "id": "5ede1268f363436aae8e52b0",
    "firstname": "adam",
    "lastname": "nebli",
    "email": "h@gmail.com"
 }

我的问题是我想要一个不像那样的简单 id,因为我需要在我的编辑方法中使用它。我想创建一个这样的方法:

 void editTemplate ( String id , Template temp) {
 template = repo.findbyTempId(id);
 template.setemail(temp.getemail());
 template.setlastname(temp.getlastname());
 }

如何设置 mongo 以创建具有简单 id 而不是 Objectid 的集合?

【问题讨论】:

    标签: mongodb spring-boot


    【解决方案1】:

    您可以转换 mongoDB 提供的 id。为此使用模式:https://mongoosejs.com/docs/guide.html

    const testSchema = new mongoose.Schema({ 电子邮件: { 类型:字符串, 要求:真, }, 姓: { 类型:字符串, 要求:真, } }) testSchema.set('toJSON', { 转换:(文档,返回对象)=> { 返回对象.id = 返回对象._id.toString() 删除返回的Object._id 删除返回的对象.__v } }) module.exports = mongoose.model('Test', testSchema)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2023-03-28
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多