【问题标题】:Mongoose: doesn't put _id to embedded documentMongoose:不将 _id 放入嵌入式文档
【发布时间】:2015-08-05 19:47:25
【问题描述】:
var Embedded = new Schema({
   some: String
})

var Main = new Schema({
  other: String,
  em: [Embedded]
})

在 Main.save({other:1, em:[{some:2}]}) mongoose 添加对象 {other:1, em:[{some:2,"_id" : ObjectId("51f6d89a6269170000000039 ")}]} 到数据库。

我可以告诉猫鼬不要在嵌入文档中添加_id吗?

【问题讨论】:

  • 嘿@adsurbum 我的回答有用吗?如果是这样,如果您接受,我会很高兴:)

标签: node.js mongodb mongoose


【解决方案1】:

定义架构时,您可以指定选项作为第二个参数。将 _id 设置为 false 以禁用自动 _id。

var Embedded = new Schema({
  some: String
}, {
  _id: false
})

the docs

【讨论】:

  • 那该死的完全相反的呢?我花了最后半天时间试图设置_id
猜你喜欢
  • 2014-01-04
  • 2011-12-21
  • 2017-06-22
  • 1970-01-01
  • 2011-03-10
  • 2016-02-23
  • 2011-09-01
  • 2015-10-14
  • 2014-01-29
相关资源
最近更新 更多