开发者

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

开发者 https://www.devze.com 2023-03-30 09:58 出处:网络
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 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.

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 iden开发者_C百科tifiers don't show up as embedded documents when I call 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. After playing around with the Mongo Driver to retrieve records Mongoid had created I discovered that I had everything right but the attribute name.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消