阻止猫鼬为子文档数组项创建_id属性 [英] Stop Mongoose from creating _id property for sub-document array items
本文介绍了阻止猫鼬为子文档数组项创建_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?
推荐答案
很简单,您可以在子模式中定义它:
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);
这篇关于阻止猫鼬为子文档数组项创建_id属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文