gamnit: fix virtual gamepad asset name
[nit.git] / lib / mongodb / queries.nit
index 10037db..4b8e9a1 100644 (file)
@@ -87,7 +87,7 @@ class MongoMatch
                        q["${name}"] = value
                        self[field] = q
                else
-                       self[name] = value
+                       self["${name}"] = value
                end
                return self
        end
@@ -211,7 +211,7 @@ class MongoMatch
        # `$in` selects the documents where the value of a field equals any value
        # in the specified array.
        fun is_in(field: String, values: Array[nullable Jsonable]): MongoMatch do
-               op("$in", field, new JsonArray.from(values))
+               op("in", field, new JsonArray.from(values))
                return self
        end
 
@@ -227,7 +227,7 @@ class MongoMatch
        # * the field value is not in the specified array or
        # * the field does not exist.
        fun is_nin(field: String, values: Array[nullable Jsonable]): MongoMatch do
-               op("$nin", field, new JsonArray.from(values))
+               op("nin", field, new JsonArray.from(values))
                return self
        end
 
@@ -245,7 +245,7 @@ class MongoMatch
        # { field: { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } }
        # ~~~
        fun lor(field: nullable String, expressions: Array[Jsonable]): MongoMatch do
-               op("$or", field, new JsonArray.from(expressions))
+               op("or", field, new JsonArray.from(expressions))
                return self
        end
 
@@ -262,7 +262,7 @@ class MongoMatch
        # { field: { $and: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } }
        # ~~~
        fun land(field: nullable String, expressions: Array[Jsonable]): MongoMatch do
-               op("$and", field, new JsonArray.from(expressions))
+               op("and", field, new JsonArray.from(expressions))
                return self
        end
 
@@ -280,7 +280,7 @@ class MongoMatch
        # { field: { $not: { <expression> } } }
        # ~~~
        fun lnot(field: nullable String, expression: Jsonable): MongoMatch do
-               op("$not", field, expression)
+               op("not", field, expression)
                return self
        end
 
@@ -298,7 +298,51 @@ class MongoMatch
        # { field: { $nor: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } }
        # ~~~
        fun lnor(field: nullable String, expressions: Array[Jsonable]): MongoMatch do
-               op("$nor", field, new JsonArray.from(expressions))
+               op("nor", field, new JsonArray.from(expressions))
+               return self
+       end
+
+       # Array contains all
+       #
+       # https://docs.mongodb.com/manual/reference/operator/query/all/#op._S_all
+       #
+       # `$all` selects the documents where the value of a field is an array that
+       # contains all the specified elements.
+       #
+       # ~~~json
+       # { field: { $all: [ <value1>, <value2>, ... ] } }
+       # ~~~
+       fun all(field: nullable String, values: Array[Jsonable]): MongoMatch do
+               op("all", field, new JsonArray.from(values))
+               return self
+       end
+
+       # Array element match
+       #
+       # https://docs.mongodb.com/manual/reference/operator/query/elemMatch/#op._S_elemMatch
+       #
+       # `$elemMatch` matches documents that contain an array field with at least
+       # one element that matches all the specified query criteria.
+       #
+       # ~~~json
+       # { field: { $elemMatch: <query> } }
+       # ~~~
+       fun elem_match(field: nullable String, query: Jsonable): MongoMatch do
+               op("elemMatch", field, query)
+               return self
+       end
+
+       # Array size match
+       #
+       # https://docs.mongodb.com/manual/reference/operator/query/size/#op._S_size
+       #
+       # `$size` matches any array with the number of elements specified by the argument
+       #
+       # ~~~json
+       # { field: { $size: <size> } }
+       # ~~~
+       fun size(field: nullable String, size: Int): MongoMatch do
+               op("size", field, size)
                return self
        end
 end
@@ -435,6 +479,20 @@ class MongoPipeline
        # { $group: { <group> } }
        # ~~~
        fun group(group: MongoGroup): MongoPipeline do return add_stage("group", group)
+
+       # Apply unwind
+       #
+       # https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
+       #
+       # Deconstructs an array field from the input documents to output a document
+       # for each element.
+       # Each output document is the input document with the value of the array
+       # field replaced by the element.
+       #
+       # ~~~json
+       # { $unwind: <field path> }
+       # ~~~
+       fun unwind(path: String): MongoPipeline do return add_stage("unwind", path)
 end
 
 # Mongo pipeline group stage