MongoMapper 禁用嵌入式 ID
是否可以禁用嵌入文档的 _id ?
例如,如果父文档具有嵌入的子文档。如果我不打算从其他地方引用孩子,那么孩子拥有身份证似乎毫无意义。
class Parent
include MongoMapper::Document
one :child
key :name
end
class Child
include MongoMapper::EmbeddedDocument
key :name
end
parent = Parent.new(:name => 'parent',
:child => Child.new(:name => 'child'))
puts parent.to_json
{"id":"4ebeddde51d9e56dcb000006","name":"parent",
"child":{
"id":"4ebeddde51d9e56dcb000005",
"name":"child"}}
Is it possible to disable _id for embedded documents?
For example if have a parent with an embedded child document. it seems kind of pointless for the child to have an id if i don't intend to reference the child from anywhere else.
class Parent
include MongoMapper::Document
one :child
key :name
end
class Child
include MongoMapper::EmbeddedDocument
key :name
end
parent = Parent.new(:name => 'parent',
:child => Child.new(:name => 'child'))
puts parent.to_json
{"id":"4ebeddde51d9e56dcb000006","name":"parent",
"child":{
"id":"4ebeddde51d9e56dcb000005",
"name":"child"}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我记得有人曾经讨论过要删除嵌入式字段的 _id 字段。最好的办法是自己修补它,然后发送拉取请求。
否则,没有用于删除 id 的配置选项。如果你敢于冒险,你也许可以覆盖 to_mongo 。
I remember there being talk at one point of removing the _id field for embedded one. Best bet would be to patch it yourself and then send a pull request.
Otherwise, there isn't a config option to remove the id. You might be able to over-write to_mongo, if you were adventurous.