【问题标题】:mongoid document to_json including all embedded documents without ':include' on each onemongoid 文档 to_json 包括所有没有':include' 的嵌入文档
【发布时间】:2011-12-08 15:36:59
【问题描述】:

给定一个任意的 mongoid 文档,我如何将其转换为 JSON 并包含任何嵌入式结构,而无需在我的 to_json 语句中专门包含这些结构。

例如:

#!/usr/bin/env ruby
require 'mongoid'
require 'json'
require 'pp'

class Doc
  include Mongoid::Document
  include Mongoid::Timestamps

  field :doc_specific_info    , type: String 

  embeds_many :persons
end

class Person
  include Mongoid::Document

  field :role                  , type: String
  field :full_name             , type: String

  embeds_many :addresses
  embedded_in :Doc
end

class Address
  include Mongoid::Document

  field :full_address       , type: String

end

doc = Doc.new
doc.doc_specific_info = "TestReport"

p = Person.new
p.role = 'buyer'
p.full_name = 'JOHN DOE'
doc.persons << p

a = Address.new
a.full_address =  '1234 nowhere ville' 
doc.persons.first.addresses << a

# THIS STATEMENT
pp JSON.parse(doc.to_json(:include => { :persons => { :include => :addresses } }  ) )
#   GIVES ME
#   {"_id"=>"4ee0d30fab1b5c5743000001",
#    "created_at"=>nil,
#    "doc_specific_info"=>"TestReport",
#    "updated_at"=>nil,
#    "persons"=>
#     [{"_id"=>"4ee0d30fab1b5c5743000002",
#       "full_name"=>"JOHN DOE",
#       "role"=>"buyer",
#       "addresses"=>
#        [{"_id"=>"4ee0d30fab1b5c5743000003",
#          "full_address"=>"1234 nowhere ville"}]}]}

# THIS STATEMENT
pp JSON.parse(doc.to_json() )
#  GIVES ME
#  {"_id"=>"4ee0d2f8ab1b5c573f000001",
#   "created_at"=>nil,
#    "doc_specific_info"=>"TestReport",
#     "updated_at"=>nil}

所以我想要的是这样的声明:

   # FOR A STATEMENT LIKE THIS
    pp JSON.parse(doc.to_json( :everything }  ) )
    #   TO GIVE ME THE COMPLETE DOCUMENT LIKE SO:
    #   {"_id"=>"4ee0d30fab1b5c5743000001",
    #    "created_at"=>nil,
    #    "doc_specific_info"=>"TestReport",
    #    "updated_at"=>nil,
    #    "persons"=>
    #     [{"_id"=>"4ee0d30fab1b5c5743000002",
    #       "full_name"=>"JOHN DOE",
    #       "role"=>"buyer",
    #       "addresses"=>
    #        [{"_id"=>"4ee0d30fab1b5c5743000003",
    #          "full_address"=>"1234 nowhere ville"}]}]}

这样的说法存在吗?如果不是,那么我唯一的选择是撤回文件的结构并产生正确的包括我自己?如果有其他方法可以更好地可视化整个文档?

【问题讨论】:

  • 你试过doc.as_document.to_json
  • @rubish - 有没有办法包含特定的嵌入文档,而不是全部?
  • @Nick 我不知道有这样的事情,但是doc.as_document 给了你一个哈希值,所以你可以使用hash.only 过滤键

标签: ruby json mongoid


【解决方案1】:

论坛里的rubish回答了这个问题,但他没有发布答案,所以我正在这样做。

答案是使用“doc.as_document.as_json”,它将为您提供整个文档。

pp doc.as_document.as_json

【讨论】:

    【解决方案2】:

    您可以覆盖文档中的#to_json 方法以添加所有包含。

    class Person
    
      def to_json(*args)
        super(args.merge({:include => { :persons => { :include => :addresses } } } )
      end
    end
    

    现在你可以拥有一切

    person.to_json()
    

    如果你想返回完整的只有:everything 选项,你可以这样做:

    class Person
    
      def to_json(*args)
        if args[0] == :everything
          super(args.merge({:include => { :persons => { :include => :addresses } } } )
        else
          super(args)
        end
      end
    
    end
    

    【讨论】:

    • 这要求你对 mongoid 对象有所了解。给定构成文档的任意对象集,我想要整个事情。这个问题被rubish回答了。 doc.as_document.to_json 就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 2011-07-23
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多