猫鼬自定义_id

时间:2021-06-10 14:26:10

标签: javascript node.js mongodb mongoose mongoose-schema

我试图让一个用户使用自定义 _id,其他用户使用默认值。

const userSchema = new mongoose.Schema({
  _id: { type: String, default: mongoose.Types.ObjectId() },
  password: String,
  fName: String,
  lName: String,
  email: { type: String, unique: true, lowercase: true },
  blogs: [blogSchema],
  followers: Array,
  followings: Array,
  darkMode: Boolean,
});

自定义 id 有效,用户将 _id 作为字符串。

enter image description here

但其他值类型也是字符串而不是 ObjectId。

enter image description here

是否可以将此默认_id 转换为 ObjectId?

或者如果我把它留成字符串会有什么问题吗?

0 个答案:

没有答案