【问题标题】:How to generate auto serial number in MERN? [duplicate]如何在 MERN 中生成自动序列号? [复制]
【发布时间】:2021-12-03 15:52:35
【问题描述】:

我正在尝试为我的“ticketno” 生成一个自动序列号。当用户创建新的应用程序票数时,票数将自动增加一。谁能告诉我该怎么做?

这是数据库架构:

const mongoose = require('mongoose');

let kamFormSchema = new mongoose.Schema(
    {
        ticketno: { type: String, required: true },
        kcpname: { type: String, required: true },
        kcpcontact: { type: String, required: true },
        kcpnid: { type: String, required: true },
        companyname: { type: String, required: true },
        ccategory: { type: String, required: true },
        corporatecode: { type: String, required: true },
        ccategorytype: { type: String, required: true },
        ccategorysubtype: { type: String, required: true },
        totalemp: { type: String, required: true },
        msisdn: { type: String, required: true },
        approvecategory: { type: String, required: true },
        subcategory: { type: String, required: true },
        file: { type: String },
        date: { type: Date, required: true },
    },
    { collation: { locale: 'en' } }
);

//var KamForm = mongoose.model('kamForm', kamFormSchema);

module.exports = mongoose.model('kamForm', kamFormSchema);

【问题讨论】:

标签: javascript node.js reactjs mongodb express


【解决方案1】:
import mongoose from 'mongoose'
import {v4} from 'uuid';
await mongoose.connect("<YOUR MONGODB ADDRESS>")

const schema = new mongoose.Schema({
    serial: {
        type: String,
        default: v4
    }
})

const model = mongoose.model("test", schema)

await new model().save()

【讨论】:

  • 或者使用任意函数生成唯一id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 2016-03-21
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多