【问题标题】:Mongoose _id to unit64猫鼬 _id 到 int64
【发布时间】:2021-04-18 14:57:12
【问题描述】:

我的 MongoDB 数据库中需要一个 unit64 ID。 ObjectId 为 96 位。 我已经看到here 的答案,一种方法是在 ID 的开头添加一些常量字符。但是我是如何在 Mongoose 中实现这一点的呢?

假设我有这样的架构:

var mongoose = require('mongoose');
 
var schema = new mongoose.Schema({
 _id: {
      ???
 },
});

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    我现在最终使用nanoid

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const { customAlphabet } = require('nanoid');
    const alphabet = '123456789';
    const nanoid = customAlphabet(alphabet, 19); //from 11...111 to 99...999
    
    const mySchema = new Schema({
      int_id: {
        type: String,
        unique: true,
        default: () => nanoid()
      },
    });
    

    【讨论】:

      猜你喜欢
      • 2013-06-19
      • 2016-11-12
      • 2016-11-06
      • 2018-07-09
      • 2020-10-23
      • 2012-05-08
      • 2023-02-22
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多