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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17254008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:53:05  来源:igfitidea点击:

Stop Mongoose from creating _id property for sub-document array items

node.jsmongodbmongoosesubdocument

提问by Atlas

If you have subdocument arrays, Mongoose automatically creates ids for each one. Example:

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

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

Is there a way to tell Mongoose to not create ids for objects within an array?

有没有办法告诉 Mongoose 不为数组中的对象创建 id?

回答by throrin19

It's simple, you can define this in the subschema :

很简单,你可以在 subschema 中定义它:

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);

回答by Joel Grenon

You can create sub-documents without schema and avoid _id. Just add _id:falseto your subdocument declaration.

您可以创建没有架构的子文档并避免_id. 只需添加_id:false到您的子文档声明。

var schema = new mongoose.Schema({
   field1:{
      type:String
   },
   subdocArray:[{
      _id:false,
      field :{type:String}
   }]
});

This will prevent the creation of an _idfield in your subdoc.

这将阻止_id在您的子文档中创建字段。

Tested in Mongoose v5.9.10

在猫鼬中测试 v5.9.10

回答by wlingke

Additionally, if you use an object literal syntax for specifying a sub-schema, you may also just add _id: falseto supress it.

此外,如果您使用对象字面量语法来指定子模式,您也可以添加_id: false以抑制它。

{
   sub: {
      property1: String,
      property2: String,
      _id: false
   }
}

回答by jemiloii

I'm using mongoose 4.6.3 and all I had to do was add _id: false in the schema, no need to make a subschema.

我正在使用 mongoose 4.6.3,我所要做的就是在模式中添加 _id: false ,无需创建子模式。

{
    _id: ObjectId
    subDocArray: [
      {
        _id: false,
        field: "String"
      }
    ]
}

回答by Deeksha Sharma

You can use either of the one

您可以使用其中任何一个

var subSchema = mongoose.Schema({
//subschema fields    

},{ _id : false });

or

或者

var subSchema = mongoose.Schema({
//subschema content
_id : false    

});

Check your mongoose version before using the second option

在使用第二个选项之前检查你的猫鼬版本

回答by Oliver White

If you want to use a predefined schema (with _id) as subdocument (without _id), you can do as follow in theory :

如果您想使用预定义的模式(带 _id)作为子文档(不带 _id),理论上可以执行以下操作:

const sourceSchema = mongoose.Schema({
    key : value
})
const subSourceSchema = sourceSchema.clone().set('_id',false);

But that didn't work for me. So I added that :

但这对我不起作用。所以我补充说:

delete subSourceSchema.paths._id;

Now I can include subSourceSchema in my parent document without _id. I'm not sure this is the clean way to do it, but it work.

现在我可以在没有 _id 的情况下在我的父文档中包含 subSourceSchema。我不确定这是做到这一点的干净方式,但它有效。