是否可以从嵌入式文档中禁用/删除_id?

时间:2019-11-01 10:34:01

标签: ruby-on-rails mongoid

我想从模型中的嵌入式文档中禁用_id或将其删除。

创建文档后,我尝试使用unset操作,但_id字段仍然保留。我也不想存储散列,因为这似乎是一种可能的解决方案。

因此,如果我有以下两个类,当我在Compass中看到集合时,mongoid在嵌套的Address对象中为我提供_id字段。

class User < RootClass
  include Mongoid::Document
  store_in collection: "users_table"
  field :_id, type: String, default: ->{ userName }
  field :userName, type: String
  embeds_one :address, class_name: 'Address', cascade_callbacks: true
  accepts_nested_attributes_for :address, allow_destroy: true
end

class Address < RootClass
  include Mongoid::Document
  embedded_in :User
  field :address_line_one, type: String
  field :address_line_two, type: String
  field :address_line_three, type: String
end

{
  "_id": "Person 1",
  "address": {
    "_id": 0, // I want to disable/remove this
    "address_line_one": "Address line one",
    "address_line_two": "Address line two",
    "address_line_three": "Address line three",

  }
}

0 个答案:

没有答案
相关问题