计算 Mongoid 中的嵌入式文档
Counting embedded documents in Mongoid
我正在使用 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
这是一个示例 sheet 文档 (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
}
]
}
]
}
我正在尝试 return 显示值的计数,但失败了。我已经尝试将聚合框架与 Mongoid 一起使用,但它似乎 return 没有任何结果。
我正在使用以下代码进行聚合,但我觉得我做错了什么。我没有从 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)
当我检查结果时,它 return 是一个 Mongo::Collection::View::Aggregation
对象,我不知道如何处理它。
任何帮助将不胜感激
谢谢
好的。一些开始的提示:
1) #collection 转到那个 class 的集合对象。所以 Sheet.collection 与 sheet.first.collection 相同。它是整个文档集合的句柄。
2) 调用输出时聚合只有运行。放一个 .each{|l| p l } 你会看到它吐出了结果
3) 聚合可以由无限数量的操作组成。传递到聚合中的数组按照您发送的顺序执行。因此 [project, match, sort] 将 return 一组不同的结果到 [match, sort, project]。您可以边构建边输出图层,以确保构建正确。
4) 您需要展开嵌入的文档以执行您希望的计数 (https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/)。
如果您需要我为您编写聚合,我可以,但是完成自己构建聚合的动作是一个很好的练习。您将在 mongodb 网站上找到您需要的所有信息。如果您需要任何帮助,请大声喊叫。
我正在使用 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
这是一个示例 sheet 文档 (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
}
]
}
]
}
我正在尝试 return 显示值的计数,但失败了。我已经尝试将聚合框架与 Mongoid 一起使用,但它似乎 return 没有任何结果。
我正在使用以下代码进行聚合,但我觉得我做错了什么。我没有从 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)
当我检查结果时,它 return 是一个 Mongo::Collection::View::Aggregation
对象,我不知道如何处理它。
任何帮助将不胜感激
谢谢
好的。一些开始的提示:
1) #collection 转到那个 class 的集合对象。所以 Sheet.collection 与 sheet.first.collection 相同。它是整个文档集合的句柄。
2) 调用输出时聚合只有运行。放一个 .each{|l| p l } 你会看到它吐出了结果
3) 聚合可以由无限数量的操作组成。传递到聚合中的数组按照您发送的顺序执行。因此 [project, match, sort] 将 return 一组不同的结果到 [match, sort, project]。您可以边构建边输出图层,以确保构建正确。
4) 您需要展开嵌入的文档以执行您希望的计数 (https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/)。
如果您需要我为您编写聚合,我可以,但是完成自己构建聚合的动作是一个很好的练习。您将在 mongodb 网站上找到您需要的所有信息。如果您需要任何帮助,请大声喊叫。