如果您有子文档数组,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);