阻止Mongoose为子文档数组项创建_id属性
如果您有子文档数组,Mongoose会自动为每个数组创建ID。例:
{ _id: "mainId"
subDocArray: [
{
_id: "unwantedId",
field: "value"
},
{
_id: "unwantedId",
field: "value"
}
]
}
有没有办法告诉猫鼬不要为数组内的对象创建ID?
回答:
很简单,您可以在以下子模式中定义它:
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);
以上是 阻止Mongoose为子文档数组项创建_id属性 的全部内容, 来源链接: utcz.com/qa/409887.html