简体   繁体   English

如何使用Ruby API在MongoDB中嵌入多个文档?

[英]How do I embed multiple documents in MongoDB using Ruby API?

I'm trying to insert a document that has multiple embedded documents but I have been unable to determine the structure for such a document. 我正在尝试插入具有多个嵌入式文档的文档,但无法确定此类文档的结构。

I'm using Mongoid in most places but need to perform a batch document insert. 我在大多数地方都使用Mongoid,但需要执行批处理文档插入。

I've tried the following: 我尝试了以下方法:

    def build_records_array(records)
        records.collect do |record|
            record.raw_attributes["identifier"] = record.identifiers.collect { |identifier| identifier.raw_attributes }
            record.raw_attributes
        end
    end # self.build_records_array

However the identifiers don't show up as embedded documents when I call insert. 但是,当我调用insert时,标识符不会显示为嵌入式文档。 I just get a bunch of garbage in my parent document. 我的父文档中只有一堆垃圾。

What is the proper structure for embedded documents? 嵌入式文档的正确结构是什么?

So, I just had a typo. 所以,我刚打错字。 I wasn't thinking about Mongoid when looking at my problem. 看着我的问题时,我并没有考虑过Mongoid。 After playing around with the Mongo Driver to retrieve records Mongoid had created I discovered that I had everything right but the attribute name. 在与Mongo驱动程序一起玩耍以检索Mongoid创建的记录后,我发现除了属性名称之外,我一切都正确。

def build_records_array(records)
  records.collect do |record|
    record.raw_attributes["identifiers"] = record.identifiers.collect { |identifier| identifier.raw_attributes }
    record.raw_attributes
  end
end # build_records_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM