【问题标题】:Counting embedded documents in Mongoid计算 Mongoid 中的嵌入文档
【发布时间】:2016-08-11 23:48:32
【问题描述】:

我正在使用 Rails + Mongoid,我有 3 个模型设置如下:

# sheet.rb
class Sheet
  include Mongoid::Document

  field :name, type: String

  embeds_many :rows
end

# row.rb
class Row
  include Mongoid::Document

  field :name, type: String

  embedded_in :sheet
  embeds_many :cells
end

# cell.rb
class Cell
  include Mongoid::Document

  field :display_value, type: String
  field :column_id, type: String
  field :active, type: Mongoid::Boolean

  embedded_in :row
end

这是一个示例工作表文档 (JSON):

{
    "_id" : ObjectId("57100b713ab82964c3c17ecb"),
    "name" : "Ship Tracker",
  "rows": [
    {
        "_id" : ObjectId("57100b813ab82964c3c17f54"),
      "name": "Obelisk"
        "cells" : [
            {
                "_id" : ObjectId("57100b813ab82964c3c17f55"),
                "column_id": "7263313013827459",
                "display_value" : "Undocked",
                "active": true
            },
            {
                "_id" : ObjectId("57100b813ab82964c3c17f76"),
                "column_id" : "7263313013827460",
                "display_value" : "J7X-VN",
                "active": true
            }
        ]
    },
    {
        "_id" : ObjectId("57100b813ab82964c3c18e3a"),
      "name": "Thanatos"
        "cells" : [
            {
                "_id" : ObjectId("57100b813ab82964c3c17f6e"),
                "column_id": "7263313013827459",
                "display_value" : "Undocked",
                "active": true
            },
            {
                "_id" : ObjectId("57100b813ab82964c3c17f70"),
                "column_id" : "7263313013827460",
                "display_value" : "NHKO-4",
                "active": true
            }
        ]
    },
    {
        "_id" : ObjectId("57100b813ab82964c3c17f47"),
      "name": "Brutix"
        "cells" : [
            {
                "_id" : ObjectId("57100b813ab82964c3c17f66"),
                "column_id": "7263313013827459",
                "display_value" : "Docked",
                "active": true
            },
            {
                "_id" : ObjectId("57100b813ab82964c3c17f3c"),
                "column_id" : "7263313013827460",
                "display_value" : "P-T9VC",
                "active": true
            }
        ]
    }
  ]
}

我正在尝试返回显示值的计数,但失败了。我曾尝试将聚合框架与 Mongoid 一起使用,但它似乎没有返回任何结果。

我正在使用以下代码进行聚合,但我觉得我做错了什么。我没有从 Mongoid/Ruby 收到任何错误:

 query = [{'$match': {'$rows.$cells.active': true}},
        {'$group': {'_id': '$rows.$cells.display_value', 'total': {'$sum': '$amount'}}}]
 sheet = Sheet.first
 results = sheet.collection.aggregate(query)

当我检查结果时,它返回一个Mongo::Collection::View::Aggregation 对象,我不知道该怎么处理它。

任何帮助将不胜感激

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby mongodb mongoid aggregation-framework


    【解决方案1】:

    好的。几点开始:

    1) #collection 转到该类的集合对象。所以 Sheet.collection 和 sheet.first.collection 是一样的。它是整个文档集合的句柄。

    2) 聚合仅在调用输出时运行。放一个 .each{|l| p l } 你会看到它吐出结果

    3) 聚合可以由无限数量的操作组成。传入聚合的数组按照您发送它的顺序执行。因此 [project, match, sort] 将返回一组不同的结果给 [match, sort, project]。您可以在进行过程中构建和输出图层,以确保正确构建它。

    4) 您需要展开嵌入的文档以进行您希望的计数 (https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/)。

    如果您需要我为您编写聚合,我可以,但是自己完成构建它的动作是一个很好的练习。您可以在 mongodb 站点上找到所需的所有信息。如果您需要任何帮助,请大声喊叫。

    【讨论】:

    • 不错。这是完美的。我设法得到了我需要的东西。 unwind 运算符是我一直在寻找的。这确实是一个很好的学习练习。谢谢,伙计。
    猜你喜欢
    • 2011-08-19
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多