阻止 Mongoose 为子文档数组项创建 _id 属性 [英] Stop Mongoose from creating _id property for sub-document array items
本文介绍了阻止 Mongoose 为子文档数组项创建 _id 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果您有子文档数组,Mongoose 会自动为每个数组创建 id.示例:
If you have subdocument arrays, Mongoose automatically creates ids for each one. Example:
{
_id: "mainId"
subDocArray: [
{
_id: "unwantedId",
field: "value"
},
{
_id: "unwantedId",
field: "value"
}
]
}
有没有办法告诉 Mongoose 不要为数组中的对象创建 id?
Is there a way to tell Mongoose to not create ids for objects within an array?
推荐答案
很简单,你可以在 subschema 中定义:
It's simple, you can define this in the 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);
这篇关于阻止 Mongoose 为子文档数组项创建 _id 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文