使用cuid来replacemongoose ObjectIds

我正在考虑使用cuid( https://www.npmjs.com/package/cuid )来replacemongoose的自然生成的objectids,这样我的模型中的_id就会变成cuid而不是生成的。

问题:

  1. 你怎么做到这一点
  2. 有没有什么副作用
  3. 这将如何影响人口?

1是如此简单:

 import mongoose from 'mongoose'; import cuid from 'cuid'; const Schema = mongoose.Schema; export const departmentSchema = new Schema({ _id: { type: 'String', default: cuid(), required: true }, name: { type: 'String', required: true }, sector: { type: 'String', required: true }, }); 

??

谢谢

我已经成功使用以下来使用CUID而不是_id:

 import mongoose from 'mongoose'; import cuid from 'cuid'; const gymSchema = new Schema({ _id: { type: 'String', required: true, default: cuid }, }): 

不同的是,我只是通过函数,而你调用函数。 起初我犯了这个错误

我可以肯定人口是否按预期工作,但是我不能回答的问题是否有任何副作用。 可能更重要的是,为什么你会这么做。 mern.io使用CUID而不是ObjectId ,但是他们在另一个领域做的不好。 我不知道为什么ObjectId是不合适的。 任何人?