- Reference
- Query DSL
Query DSL
Casbah provides a rich fluid query syntax, that allows you to construct
DBObjects on the fly using MongoDB query operators.
Query Selectors
Comparison Operators
$allMatches arrays that contain all elements specified:"size" $all ("S", "M", "L")$eqMatches values that are equal to the value specified:"price" $eq 10$gtMatches values that are greater than the value specified in the query$gteMatches values that are equal to or greater than the value specified:"price" $gt 10 "price" $gte 10$inMatches any of the values that exist in an array specified:"size" $in ("S", "M", "L")$ltMatches values that are less than the value specified in the query$lteMatches values that are less than or equal to the value specified:"price" $lt 100 "price" $lte 100$neMatches all values that are not equal to the value specified:"price" $ne 1000$ninMatches values that do not exist in an array specified:"size" $nin ("S", "XXL")
Logical Operators
$orJoins query clauses with a logicalORreturns all documents that match the conditions of either clause:$or( "price" $lt 5 $gt 1, "promotion" $eq true ) $or( ( "price" $lt 5 $gt 1 ) :: ( "stock" $gte 1 ) )$andJoins query clauses with a logicalANDreturns all documents that match the conditions of both clauses:$and( "price" $lt 5 $gt 1, "stock" $gte 1 ) $and( ( "price" $lt 5 $gt 1 ) :: ( "stock" $gte 1 ) )$notInverts the effect of a query expression and returns documents that do not match the query expression:"price" $not { _ $gte 5.1 }$norJoins query clauses with a logicalNORreturns all documents that fail to match both clauses:$nor( "price" $eq 1.99 , "qty" $lt 20, "sale" $eq true ) $nor( ( "price" $lt 5 $gt 1 ) :: ( "stock" $gte 1 ) )
Element Operators
$existsMatches documents that have the specified field:"qty" $exists true$modPerforms a modulo operation on the value of a field and selects documents with a specified result:"qty" $mod (5, 0)$typeSelects documents if a field is of the specified type:"size".$type[BasicDBList]
JavaScript Operators
$whereMatches documents that satisfy a JavaScript expression:$where("function () { this.credits == this.debits }")$regexSelects documents where values match a specified regular expression. You can also use native regular expressions:"foo" $regex "^bar$" "foo" $eq "^bar$".r
Geospatial Operators
$geoWithinSelects geometries within a bounding GeoJSON geometry:// Create a GeoJson geometry document var geo = MongoDBObject("$geometry" -> MongoDBObject("$type" -> "polygon", "coordinates" -> (((GeoCoords(74.2332, -75.23452), GeoCoords(123, 456), GeoCoords(74.2332, -75.23452)))))) // Example $geoWithin Queries "location" $geoWithin(geo) "location" $geoWithin $box ((74.2332, -75.23452), (123, 456)) "location" $geoWithin $center ((50, 50), 10) "location" $geoWithin $centerSphere ((50, 50), 10)$geoIntersectsSelects geometries that intersect with a GeoJSON geometry:// Create a GeoJson geometry document var geo = MongoDBObject("$geometry" -> MongoDBObject("$type" -> "polygon", "coordinates" -> (((GeoCoords(74.2332, -75.23452), GeoCoords(123, 456), GeoCoords(74.2332, -75.23452)))))) val near = "location" $geoIntersects geo$nearReturns geospatial objects in proximity to a point:"location" $near (74.2332, -75.23452)$nearSphereReturns geospatial objects in proximity to a point on a sphere:"location" $nearSphere (74.2332, -75.23452)
Array Query Operators
$elemMatchSelects documents if element in the array field matches all the specified$elemMatchconditions:"colour" $elemMatch MongoDBObject("base" -> "red", "flash" -> "silver")$sizeSelects documents if the array field is a specified size:"comments" $size 12
Update DSL Operators
Field Operators
$incIncrements the value of the field by the specified amount:$inc("sold" -> 1, "stock" -> -1)$renameRenames a field:$rename("color" -> "colour", "realize" -> "realise")$setOnInsertSets the value of a field upon documentation creation during an upsert. Has no effect on update operations that modify existing documents:$setOnInsert("promotion" -> "new")$setSets the value of a field in an existing document:$set("promotion" -> "sale", "qty" -> 100)$unsetRemoves the specified field from an existing document:$unset("promotion")$currentDateUpdates the specified fields to current server-side date or timestamp (the only allowed values):$currentDate("lastUpdated" -> "date", "ts" -> "timestamp")
Array Update Operators
$addToSetAdds elements to an existing array only if they do not already exist in the set:$addToSet("sizes" -> "L", "colours" -> "Blue") $addToSet("sizes") $each ("S", "M", "L", "XL")$popRemoves the first or last item of an array:$pop("sizes" -> "L")$pullRemoves items from an array that match a query statement:$pull("sizes" -> "L") $pull("widgets" $gt 2 )$pullAllRemoves multiple values from an array:$pullAll("sizes" -> ("S", "XL"))$pushAdds an item to an array:$push("sizes" -> "L") $push("widgets" $gt 2 ) $push("sizes") $each ("S", "M", "L", "XL")$pushAllDeprecated. Adds several items to an array:$pushAll("sizes" -> ("S", "XL"))
Bitwise Operators
$bitPerforms bitwiseANDandORupdates of integer values:$bit("foo") and 5
For the full query syntax to MongoDB see the core docs at: docs.mongodb.org