简体   繁体   English

如何为 mongo-DB 中的文档设置自定义 _id

[英]how can I set custom _id for a document in mongo-DB

I want to upload a data to mongodb and set a custom id for content in a file which is a sub-document in the collection--我想将数据上传到 mongodb 并为文件中的内容设置自定义 id,该文件是集合中的子文档--

{
   _id: document id...,
   FirstName: ...,
   LasrName: ...,
   Email: ...,
   files: [
   content: [
    _id: objectId(...)

] ], ] ],

} }

you can set a custom Id for the creation of data by using this schema here.您可以在此处使用此架构为创建数据设置自定义 ID。 nanoid is rundom string. nanoid 是随机字符串。

const mySchema = new Schema({
  _id: {
    type: String,
    default: () => nanoid()  /// random_string
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM