如果您有子文档数组,Mongoose 会自动为每个数组创建 id。例子:
{ _id: "mainId" subDocArray: [ { _id: "unwantedId", field: "value" }, { _id: "unwantedId", field: "value" } ] }
有没有办法告诉 Mongoose 不要为数组中的对象创建 id?
这很简单,您可以在 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);