https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group
Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping.
var group = (new MongoGroup("$game._id")).sum("nitcoins", "$game.nitcoins")
var pipeline = (new MongoPipeline).group(group)
The output documents contain an _id
field which contains the distinct group by key.
The output documents can also contain computed fields that hold the values of
some accumulator expression grouped by the $group
‘s _id
field.
$group
does not order its output documents.
The $group
stage has the following prototype form:
{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
The _id
field is mandatory; however, you can specify an _id
value of null
to calculate accumulated values for all the input documents as a whole.
The remaining computed fields are optional and computed using the <accumulator>
operators.
mongodb :: MongoGroup :: addToSet
Push to a unique arraymongodb :: MongoGroup :: avg
Returns the average value of the numeric valuesmongodb :: MongoGroup :: defaultinit
mongodb :: MongoGroup :: first
Return the first valuemongodb :: MongoGroup :: last
Return the last valuemongodb :: MongoGroup :: max
Returns the maximum valuemongodb :: MongoGroup :: min
Returns the minimum valuemongodb :: MongoGroup :: push
Push to an arraymongodb :: MongoGroup :: sum
Calculates and returns the sum of numeric valuesmongodb $ MongoGroup :: SELF
Type of this instance, automatically specialized in every classmongodb $ MongoGroup :: init
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackmongodb :: MongoGroup :: addToSet
Push to a unique arrayserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
mongodb :: MongoGroup :: avg
Returns the average value of the numeric valuescore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
json :: JsonObject :: defaultinit
json :: JsonMapRead :: defaultinit
core :: HashMap :: defaultinit
mongodb :: MongoGroup :: defaultinit
core :: Map :: defaultinit
core :: Object :: defaultinit
core :: MapRead :: defaultinit
core :: MapRead :: filter_keys
Return all elements ofkeys
that have a value.
mongodb :: MongoGroup :: first
Return the first valueserialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: MapRead :: get_or_default
Get the item atkey
or return default
if not in map
core :: MapRead :: get_or_null
Get the item atkey
or null if key
is not in the map.
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: MapRead :: keys_sorted_by_values
Return an array of all keys sorted with their values usingcomparator
.
mongodb :: MongoGroup :: last
Return the last valuecore :: MapRead :: lookup_all_values
Search all the values inpe.greaters
.
core :: MapRead :: lookup_values
Combine the values inpe.greaters
from the most smaller elements that have a value.
mongodb :: MongoGroup :: max
Returns the maximum valuemongodb :: MongoGroup :: min
Returns the minimum valueserialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).core :: MapRead :: provide_default_value
Called by the underling implementation of[]
to provide a default value when a key
has no value
mongodb :: MongoGroup :: push
Push to an arrayserialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
mongodb :: MongoGroup :: sum
Calculates and returns the sum of numeric valuescore :: MapRead :: to_map_comparator
A comparator that compares things with their values in self.serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
core :: MapRead :: values_sorted_by_key
Return an array of all values sorted with their keys usingcomparator
.
json :: JsonMapRead
A map that can be translated into a JSON object.Serializer::serialize
# Mongo pipeline group stage
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pipe._S_group
#
# Groups documents by some specified expression and outputs to the next stage a
# document for each distinct grouping.
#
# ~~~
# var group = (new MongoGroup("$game._id")).sum("nitcoins", "$game.nitcoins")
#
# var pipeline = (new MongoPipeline).group(group)
# ~~~
#
# The output documents contain an `_id` field which contains the distinct group by key.
# The output documents can also contain computed fields that hold the values of
# some accumulator expression grouped by the `$group`‘s `_id` field.
# `$group` does not order its output documents.
#
# The `$group` stage has the following prototype form:
#
# ~~~json
# { $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }
# ~~~
#
# The `_id` field is mandatory; however, you can specify an `_id` value of null
# to calculate accumulated values for all the input documents as a whole.
#
# The remaining computed fields are optional and computed using the `<accumulator>`
# operators.
class MongoGroup
super JsonObject
# Group `_id`
#
# See `MongoGroup::group`.
var id: String
init do self["_id"] = id
# Add an accumulator
#
# Each accumulator is registered as:
# ~~~json
# <field>: { <accumulator> : <expression> }
# ~~~
private fun acc(name: String, field: String, expression: nullable Serializable): MongoGroup do
var q = new JsonObject
q["${name}"] = expression
self[field] = q
return self
end
# Calculates and returns the sum of numeric values
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/sum/#grp._S_sum
#
# ~~~json
# { $sum: <expression> }
# ~~~
#
# `$sum` ignores non-numeric values.
fun sum(field: String, expression: Serializable): MongoGroup do
return acc("sum", field, expression)
end
# Returns the average value of the numeric values
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/avg/
#
# ~~~json
# { $avg: <expression> }
# ~~~
#
# `$avg` ignores non-numeric values.
fun avg(field: String, expression: Serializable): MongoGroup do
return acc("avg", field, expression)
end
# Returns the maximum value
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/max/
#
# ~~~json
# { $max: <expression> }
# ~~~
#
# `$max` compares both value and type, using the specified BSON comparison
# order for values of different types.
fun max(field: String, expression: Serializable): MongoGroup do
return acc("max", field, expression)
end
# Returns the minimum value
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/min/
#
# ~~~json
# { $min: <expression> }
# ~~~
#
# `$min` compares both value and type, using the specified BSON comparison
# order for values of different types.
fun min(field: String, expression: Serializable): MongoGroup do
return acc("min", field, expression)
end
# Return the first value
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/first/
#
# ~~~json
# { $first: <expression> }
# ~~~
#
# Returns the value that results from applying an expression to the first
# document in a group of documents that share the same group by key.
#
# Only meaningful when documents are in a defined order.
fun first(field: String, expression: Serializable): MongoGroup do
return acc("first", field, expression)
end
# Return the last value
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/last/
#
# ~~~json
# { $last: <expression> }
# ~~~
#
# Returns the value that results from applying an expression to the last
# document in a group of documents that share the same group by key.
#
# Only meaningful when documents are in a defined order.
fun last(field: String, expression: Serializable): MongoGroup do
return acc("last", field, expression)
end
# Push to an array
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/push/
#
# ~~~json
# { $push: <expression> }
# ~~~
#
# Returns an array of all values that result from applying an expression to
# each document in a group of documents that share the same group by key.
fun push(field: String, expr: Serializable): MongoGroup do
return acc("push", field, expr)
end
# Push to a unique array
#
# https://docs.mongodb.com/manual/reference/operator/aggregation/addToSet/
#
# ~~~json
# { $addToSet: <expression> }
# ~~~
#
# Returns an array of all unique values that results from applying an
# expression to each document in a group of documents that share the same
# group by key.
#
# Order of the elements in the output array is unspecified.
fun addToSet(field: String, expr: Serializable): MongoGroup do
return acc("addToSet", field, expr)
end
end
lib/mongodb/queries.nit:498,1--666,3