0

I need a unit64 ID in my MongoDB database. ObjectIds are 96 bits. I have seen the answer here that one way to do it is to add a few constant characters to the beginning of the ID. But how d I achieve this in Mongoose?

Suppose that I have a schema like this:

var mongoose = require('mongoose');
 
var schema = new mongoose.Schema({
 _id: {
      ???
 },
});
towi_parallelism
  • 1,421
  • 1
  • 16
  • 38

1 Answers1

0

I ended up using nanoid for now:

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()
  },
});
towi_parallelism
  • 1,421
  • 1
  • 16
  • 38