Database Manual / Reference / Query Language

Query Predicates查询谓词

Query predicates are expressions that return a boolean that indicates whether a document matches a specified query. For example, { name: { $eq: "Alice" } } is a query predicate that returns documents where the value of the "name" field is the string "Alice".查询谓词是返回一个布尔值的表达式,该布尔值指示文档是否与指定的查询匹配。例如,{ name: { $eq: "Alice" } }是一个查询谓词,它返回的文档中"name"字段的值是字符串"Alice"

To match the correct documents, you can use the following types of operators in query predicates:为了匹配正确的文档,您可以在查询谓词中使用以下类型的运算符:

Operator Type运算符类型Description描述
Array Query Predicate Operators数组查询谓词运算符Return data based on array conditions.根据数组条件返回数据。
Bitwise Query Predicate Operators按位查询谓词运算符Return data based on bit position conditions.根据位位置条件返回数据。
Comparison Query Predicate Operators比较查询谓词运算符Return data based on value comparisons such as less than and greater than.基于值比较(如小于和大于)返回数据。
Data Type Query Predicate Operators数据类型查询谓词运算符Return data based on field existence or data types.根据字段存在或数据类型返回数据。
Miscellaneous Query Predicate Operators其他查询谓词运算符Perform specialized functions in query predicates.在查询谓词中执行专门的函数。
Logical Query Predicate Operators逻辑查询谓词运算符Return data based on boolean logic (and, or, and nor).基于布尔逻辑(and、or和nor)返回数据。
Geospatial Query Predicate Operators地理空间查询谓词运算符Return data based on geospatial query predicates, such as containment within a region on the surface of the Earth.基于地理空间查询谓词返回数据,例如地球表面某个区域内的包含。

Alphabetical List of Operators按字母顺序排列的运算符列表

Name名称Description描述
$allMatches arrays that contain all elements specified in the query.匹配包含查询中指定的所有元素的数组。
$andJoins query clauses with a logical AND and returns documents that match the conditions of all clauses.使用逻辑AND连接查询子句,并返回符合所有子句条件的文档。
$bitsAllClearMatches numeric or binary values in which a set of bit positions all have a value of 0.匹配一组位位置都为0的数值或二进制值。
$bitsAllSetMatches numeric or binary values in which a set of bit positions all have a value of 1.匹配数值或二进制值,其中一组位位置的值都为1
$bitsAnyClearMatches numeric or binary values in which any bit from a set of bit positions has a value of 0.匹配数值或二进制值,其中一组位位置中的任一位的值为0
$bitsAnySetMatches numeric or binary values in which any bit from a set of bit positions has a value of 1.匹配数值或二进制值,其中一组位位置中的任一位的值为1
$elemMatchSelects documents if at least one element in the array field matches all the specified $elemMatch conditions.如果数组字段中至少有一个元素与所有指定的$elemMatch条件匹配,则选择文档。
$eqMatches values that are equal to a specified value.匹配等于指定值的值。
$existsMatches documents that have the specified field.匹配具有指定字段的文档。
$exprAllows use of expressions in query predicates.允许在查询谓词中使用表达式。
$geoIntersectsSelects geometries that intersect with a GeoJSON geometry. 选择与GeoJSON几何图形相交的几何图形。The 2dsphere index supports $geoIntersects.2dsphere索引支持$geoIntersects
$geoWithinSelects geometries within a bounding GeoJSON geometry. 在边界GeoJSON几何图形中选择几何图形。The 2dsphere and 2d indexes support $geoWithin.2dsphere2d索引支持$geoWithin
$gtMatches values that are greater than a specified value.匹配大于指定值的值。
$gteMatches values that are greater than or equal to a specified value.匹配大于或等于指定值的值。
$jsonSchemaValidates documents against the given JSON Schema.根据给定的JSON模式验证文档。
$inMatches any of the values specified in an array.匹配数组中指定的任何值。
$ltMatches values that are less than a specified value.匹配小于指定值的值。
$lteMatches values that are less than or equal to a specified value.匹配小于或等于指定值的值。
$modMatches documents based on the result of a modulo operation on a field value.根据字段值的模运算结果匹配文档。
$neMatches all values that are not equal to a specified value.匹配所有不等于指定值的值。
$nearReturns geospatial objects in proximity to a point. Requires a geospatial index. 返回点附近的地理空间对象。需要地理空间索引。The 2dsphere and 2d indexes support $near.2dsphere2d索引支持$near
$nearSphereReturns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. 返回球体上某点附近的地理空间对象。需要地理空间索引。The 2dsphere and 2d indexes support $nearSphere.2dsphere2d索引支持$nearSphere
$ninMatches if the value is not equal to any of a given list of values.如果该值不等于给定值列表中的任何一个,则匹配。
$norJoins query clauses with a logical NOR and returns all documents that fail to match all clauses.将查询子句与逻辑NOR连接,并返回所有与所有子句不匹配的文档。
$notInverts the effect of a query predicate and returns documents that do not match the query predicate.反转查询谓词的效果,并返回与查询谓词不匹配的文档。
$orJoins query clauses with a logical OR and returns all documents that match at least one clause.使用逻辑OR连接查询子句,并返回至少与一个子句匹配的所有文档。
$regexMatches documents where values match a specified regular expression.匹配值与指定正则表达式匹配的文档。
$sizeSelects documents if the array field contains the specified number of elements.如果数组字段包含指定数量的元素,则选择文档。
$typeMatches documents if a field is of the specified type.如果字段属于指定类型,则匹配文档。
$whereMatches documents that satisfy a JavaScript expression.匹配满足JavaScript表达式的文档。