一尘不染

阻止Mongoose为子文档数组项创建_id属性

node.js

如果您有子文档数组,Mongoose会自动为每个数组创建ID。例:

{
    _id: "mainId"
    subDocArray: [
      {
        _id: "unwantedId",
        field: "value"
      },
      {
        _id: "unwantedId",
        field: "value"
      }
    ]
}

有没有办法告诉猫鼬不要为数组内的对象创建ID?


阅读 425

收藏
2020-07-07

共1个答案

一尘不染

很简单,您可以在以下子模式中定义它:

var mongoose = require("mongoose");

var subSchema = mongoose.Schema({
    //your subschema content
},{ _id : false });

var schema = mongoose.Schema({
    // schema content
    subSchemaCollection : [subSchema]
});

var model = mongoose.model('tablename', schema);
2020-07-07