MongoDB 运算符
MongoDB查询运算符包括比较,逻辑,元素,求值,地理空间,数组,按位和注释运算符。
MongoDB比较运算符
$eq
$eq指定相等条件。它匹配字段值等于指定值的文档。
语法:
{ <field> : { $eq: <value> } }
示例:
db.books.find ( { price: { $eq: 300 } } )
上面的示例查询书集以选择价格为300的所有文档。
$gt
$gt选择字段值大于指定值的文档。
语法:
{ field: { $gt: value } }
示例:
db.books.find ( { price: { $gt: 200 } } )
$gte
$gte选择域值大于或等于指定值的文档。
语法:
{ field: { $gte: value } }
示例:
db.books.find ( { price: { $gte: 250 } } )
$in
$in运算符选择文档,其中字段的值等于指定数组中的任何值。
语法:
{ filed: { $in: [ <value1>, <value2>, ……] } }
示例:
db.books.find( { price: { $in: [100, 200] } } )
$lt
$lt运算符选择字段值小于指定值的文档。
语法:
{ field: { $lt: value } }
示例:
db.books.find ( { price: { $lt: 20 } } )
$lte
$lte运算符选择字段值小于或等于指定值的文档。
语法:
{ field: { $lte: value } }
示例:
db.books.find ( { price: { $lte: 250 } } )
$ne
$ne运算符选择字段值不等于指定值的文档。
语法:
{ <field>: { $ne: <value> } }
示例:
db.books.find ( { price: { $ne: 500 } } )
$nin
$nin运算符选择字段值不在指定数组中或不存在的文档。
语法:
{ field : { $nin: [ <value1>, <value2>, .... ] } }
示例:
db.books.find ( { price: { $nin: [ 50, 150, 200 ] } } )
MongoDB逻辑运算符
$and
$and运算符在数组上用作逻辑AND运算。数组应具有一个或多个表达式,并选择满足数组中所有表达式的文档。
语法:
{ $and: [ { <exp1> }, { <exp2> }, ....]}
示例:
db.books.find ( { $and: [ { price: { $ne: 500 } }, { price: { $exists: true } } ] } )
$not
$not运算符在指定的表达式上作为逻辑NOT起作用,并选择与该表达式无关的文档。
语法:
{ field: { $not: { <operator-expression> } } }
示例:
db.books.find ( { price: { $not: { $gt: 200 } } } )
$nor
$nor运算符在一个或多个查询表达式的数组上作为逻辑NOR起作用,并选择对该数组中所有查询表达式都失败的文档。
语法:
{ $nor: [ { <expression1> } , { <expresion2> } , ..... ] }
示例:
db.books.find ( { $nor: [ { price: 200 }, { sale: true } ] } )
$or
它在两个或多个表达式的数组上作为逻辑或运算,并选择满足至少一个表达式期望的文档。
语法:
{ $or: [ { <exp_1> }, { <exp_2> }, ... , { <exp_n> } ] }
示例:
db.books.find ( { $or: [ { quantity: { $lt: 200 } }, { price: 500 } ] } )
MongoDB元素运算符
$exists
当Boolean为true时,exists匹配包含该字段的文档。它还与字段值为空的文档匹配。
语法:
{ field: { $exists: <boolean> } }
示例:
db.books.find ( { qty: { $exists: true, $nin: [ 5, 15 ] } } )
$type
type运算符选择文档,其中字段的值是指定BSON类型的实例。
语法:
{ field: { $type: <BSON type> } }
示例:
db.books.find ( { "bookid" : { $type : 2 } } );
MongoDB评估运算符
$expr
expr运算符允许在查询语言中使用聚合表达式。
语法:
{ $expr: { <expression> } }
示例:
db.store.find( { $expr: {$gt: [ "$product" , "price" ] } } )
$jsonSchema
它与满足指定JSON Schema的文档匹配。
语法:
{ $jsonSchema: <JSON schema object> }
$mod
mod运算符选择文档,其中字段值除以除数具有指定的余数。
语法:
{ field: { $mod: [ divisor, remainder ] } }
示例:
db.books.find ( { qty: { $mod: [ 200, 0] } } )
$regex
它为查询中的模式匹配字符串提供正则表达式功能。 MongoDB使用与Perl兼容的正则表达式。
语法:
{ <field>: /pattern/<options> }
示例:
db.books.find( { price: { $regex: /789$/ } } )
$text
$text运算符在字段内容上搜索文本,并用文本索引建立索引。
语法:
{
$text:
{
$search: <string>,
$language: <string>,
$caseSensitive: <boolean>,
$diacriticSensitive: <boolean>
}
}
示例:
db.books.find( { $text: { $search: "Othelo" } } )
$where
" where"运算符用于将包含JavaScript表达式的字符串或完整的JavaScript函数传递给查询系统。
示例:
db.books.find( { $where: function() {
return (hex_md5(this.name)== "9b53e667f30cd329dca1ec9e6a8")
} } );
MongoDB地理空间
$geoIntersects
它仅选择那些地理空间数据与给定的GeoJSON对象相交的文档。
语法:
{
<location field>: {
$geoIntersects: {
$geometry: {
type: "<object type>" ,
coordinates: [ <coordinates> ]
}
}
}
}
示例:
db.places.find(
{
loc: {
$geoIntersects: {
$geometry: {
type: "Triangle" ,
coordinates: [
[ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ] ]
]
}
}
}
}
)
$geoWithin
geoWithin运算符选择的地理空间数据完全位于指定形状内的文档。
语法:
{
<location field>: {
$geoWithin: {
$geometry: {
type: <"Triangle" or "Rectangle"> ,
coordinates: [ <coordinates> ]
}
}
}
}
$near
near运算符定义地理空间查询从近到远返回文档的点。
语法:
{
<location field>: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}
示例:
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
$nearSphere
nearSphere指定一个点,地理空间查询将从该点返回文档从最近到最远。
语法:
{
$nearSphere: [ <x>, <y> ],
$minDistance: <distance in radians>,
$maxDistance: <distance in radians>
}
示例:
db.legacyPlaces.find(
{ location : { $nearSphere : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)
$all
它选择文档,其中字段的值是包含所有指定元素的数组。
语法:
{ <field>: { $all: [ <value1> , <value2> ... ] } }
示例:
db.books.find( { tags: { $all: [ "Java", "MongoDB", "RDBMS" ] } } )
$elemMatch
该运算符将包含数组字段的文档与包含至少一个与所有给定查询条件匹配的元素的文档相关。
语法:
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
示例:
db.books.find(
{ results: { $elemMatch: { $gte: 500, $lt: 400 } } }
)
$size
它选择具有参数指定元素编号的任何数组。
语法:
db.collection.find( { field: { $size: 2 } } );
MongoDB Bitwise运算符
$bitsAllClear
它与文档中查询给定的所有位位置都在字段内清晰的地方匹配。
语法:
{ <field>: { $bitsAllClear: <numeric bitmask> } }
示例:
db.inventory.find( { a: { $bitsAllClear: [ 1, 5 ] } } )
$bitsAllSet
bitallset运算符与在该字段中设置了查询给出的所有位位置的文档匹配。
语法:
{ <field>: { $bitsAllSet: <numeric bitmask> } }
示例:
db.inventory.find( { a: { $bitsAllClear: [ 1, 5 ] } } )
$bitsAnyClear
bitAnyClear运算符与文档中查询给出的任何位置在字段中清晰的匹配文档。
语法:
{ <field>: { $bitsAnyClear: <numeric bitmask> } }
示例:
db.inventory.find( { a: { $bitsAnyClear: [ 5, 10 ] } } )
$bitsAnySet
它与在字段中设置了查询给定的任何位位置的文档匹配。
语法:
{ <field>: { $bitsAnySet: <numeric bitmask> } }
示例:
db.inventory.find( { a: { $bitsAnySet: [ 1, 5 ] } } )
MongoDB注释运算符
$comment
$comment运算符将注释与带有查询谓词的任何表达式相关联。
语法:
db.inventory.find( { <query>, $comment: <comment> } )
示例:
db.inventory.find(
{
x: { $mod: [ 1, 0 ] },
$comment: "Find Odd values."
}
)
MongoDB Projection运算符
$
$运算符将查询结果中数组的内容限制为仅包含第一个元素匹配查询文档。
语法:
db.books.find( { <array>: <value> ... },
{ "<array>.$": 1 } )
db.books.find( { <array.field>: <value> ...},
{ "<array>.$": 1 } )
$elemMatch
使用此运算符从查询结果中限制数组字段的内容,使其仅包含与元素$elemMatch条件匹配的第一个元素。
语法:
db.library.find( { bookcode: "63109" },
{ students: { $elemMatch: { roll: 102 } } } )
$meta
meta运算符返回与查询相关联的元数据所在的每个匹配文档的结果。
语法:
{ $meta: <metaDataKeyword> }
示例:
db.books.find(
<query>,
{ score: { $meta: "textScore" } }
)
$slice
它控制查询返回的数组中的值数。
语法:
db.books.find( { field: value }, { array: {$slice: count } } );
示例:
db.books.find( {}, { comments: { $slice: [ 200, 100 ] } } )