Skip to content

Latest commit

 

History

History
220 lines (147 loc) · 4.59 KB

File metadata and controls

220 lines (147 loc) · 4.59 KB

Query DSL

Query Selectors

Comparison Operators

  • $eq Matches values that are equal to the value specified in the query.
"name" $eq "foo"
  • $gt Matches values that are greater than the value specified in the query.
  • $gte Matches values that are greater than or equal to the value specified in the query.
"age" $gt 18
"age" $gte 18
  • $in Matches any of the values that exist in an array specified in the query.
 "age" $in (1, 2, 3)
  • $lt Matches values that are less than the value specified in the query.
  • $lte Matches values that are less than or equal to the value specified in the query.
"age" $lt 18
"age" $lte 18
  • $ne Matches all values that are not equal to the value specified in the query.
"name" $ne "foo"
  • $nin Matches values that do not exist in an array specified to the query.
"age" $nin (1, 2, 3)

Logical Operators

  • $or Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
$or("qty" $lt 20 $gte 10, "sale" $eq true)
  • $and Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$and("name" $eq "foo", "surname" $eq "bar", "age" $eq 32)
  • $not Inverts the effect of a query expression and returns documents that do not match the query expression.
"price" $not { _ $gte 5.1 }
  • $nor Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
$nor("price" $eq 1.99, "qty" $lt 20, "sale" $eq true)

Element Operators

  • $exists Matches documents that have the specified field.
"qty" $exists true
"qty" $exists false
  • $type Selects documents if a field is of the specified type.
"qty".$type[BSONDouble]
"qty".$type[BSONNull.type]

Evaluation Operators

  • $mod Performs a modulo operation on the value of a field and selects documents with a specified result.
"qty" $mod (5, 0)
  • $regex Selects documents where values match a specified regular expression.
"name" $regex ("^Al.*", "i")
  • $text Performs text search.
$text("bake coffee cake")
$text("bake coffee cake", "turkish")
  • $where Matches documents that satisfy a JavaScript expression.
$where("function () { this.credits == this.debits }")

Array Operators

  • $all Matches arrays that contain all elements specified in the query.
"size" $all ("S", "M", "L")
  • $elemMatch Selects documents if element in the array field matches all the specified $elemMatch condition.
"array" $elemMatch ("value1" $eq 1, "value2" $gt 1)
  • $size Selects documents if the array field is a specified size.
"comments" $size 12

Update Operators

Field Update Operators

  • $inc Increments the value of the field by the specified amount.
$inc("sold" -> 1, "stock" -> -1)
  • $mul Multiplies the value of the field by the specified amount.
$mul("price" -> 1.25)
  • $rename Renames a field.
$rename("color" -> "colour", "realize" -> "realise")
  • $setOnInsert Sets the value of a field upon document creation during an upsert. Has no effect on update operations that modify existing documents.
$setOnInsert("defaultQty" -> 500, "inStock" -> true) ++ $set("item" -> "apple")
  • $set Sets the value of a field in a document.
$set("name" -> "foo", "surname" -> "bar", "age" -> 32)
  • $unset Removes the specified field from a document.
$unset("name", "surname", "age")
  • $min Only updates the field if the specified value is less than the existing field value.
$min("lowScore" -> 150)
  • $max Only updates the field if the specified value is greater than the existing field value.
$max("highScore" -> 950)
  • $currentDate Sets the value of a field to current date, either as a Date or a Timestamp.
$currentDate("lastModified" -> true, "lastModifiedTS" -> "timestamp")

Array Update Operators

  • $addToSet Adds elements to an array only if they do not already exist in the set.
$addToSet("sizes" -> "L", "colours" -> "Blue")
  • $pop Removes the first or last item of an array.
$pop("scores" -> -1)
$pop("scores" -> 1)
  • $pull Removes all array elements that match a specified query.
$pull("flags", "msr")
$pull("votes" $gte 6)
  • $push Adds an item to an array.
$push("scores", 89)