node.js - 阻止 Mongoose 为子文档数组项创建 _id 属性

标签 node.js mongodb mongoose subdocument

如果您有子文档数组,Mongoose 会自动为每个数组创建 id。示例:

{
    _id: "mainId"
    subDocArray: [
      {
        _id: "unwantedId",
        field: "value"
      },
      {
        _id: "unwantedId",
        field: "value"
      }
    ]
}

有没有办法告诉 Mongoose 不要为数组中的对象创建 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);

关于node.js - 阻止 Mongoose 为子文档数组项创建 _id 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254008/

相关文章:

node.js - 将项目添加到混合模式数组时,mongoose.save 的结果不正确

node.js - 是否可以在 Node.js 服务器重启时保持 websockets 连接?

mongodb - 从 MongoDB 中的对象数组中删除单个对象

mongodb - 从嵌入式数组中获取元素数作为响应 mongoDB

node.js - 使用不同的模式渲染 mongodb 文档

javascript - 使用 Node js在firebase web上上传文件会抛出错误firebase.storage不是函数

node.js - google.gmail() 给出错误 TypeError : google. gmail 不是函数?

java - Spring Data、Mongo 和 @TypeAlias : reading not working

javascript - 为什么 .findByIdAndUpdate 会绕过 mongoose 中间件?

javascript - Mongoose - 查找(): object inside search options is not working