lib/mongoid/report/queries_builder.rb in mongoid-report-0.0.10 vs lib/mongoid/report/queries_builder.rb in mongoid-report-0.0.11
- old
+ new
@@ -24,13 +24,21 @@
def fields
@fields ||= settings[:fields]
end
+ def in_fields
+ @in_fields ||= fields.keys
+ end
+
+ def output_fields
+ @output_fields ||= fields.values
+ end
+
def all_fields
[:_id]
- .concat(fields)
+ .concat(in_fields)
.concat(groups)
end
# Example: { '$project' => { :field1 => 1 } }
def project_query
@@ -46,11 +54,11 @@
groups.inject(query[:_id]) do |hash, group|
hash.merge!(group => GROUP_TEMPLATE % group)
end
- fields.inject(query) do |hash, field|
+ in_fields.inject(query) do |hash, field|
hash.merge!(field => { '$sum' => GROUP_TEMPLATE % field })
end
end
end
@@ -65,11 +73,11 @@
groups.inject(query) do |hash, group|
hash.merge!(group => PROJECT_TEMPLATE % group)
end
end
- fields.inject(query) do |hash, field|
- hash.merge!(field => 1)
+ fields.inject(query) do |hash, (field, name)|
+ hash.merge!(name => "$#{field}")
end
end
end
end