MongoDB simple query operator (non-aggregation operation) and mongodb Operator

Source: Internet
Author: User

MongoDB simple query operator (non-aggregation operation) and mongodb Operator
Simple query operator (non-aggregation operation)

  • Comparsion operations

(1) $ eq: used to filter the value of a key with equivalent conditions.

Usage example (filter a key that is equal to a value, you can use $ eq) db. op_test.find ({"name": {$ eq: "steven "}})

(2) $ gt: used to determine whether a key value is greater than a specified value.

Usage example (filter a key value, greater than a specified value) db. op_test.find ({"age": {$ gt: 19 }})

(3) $ gt: used to determine whether a key value is greater than or equal to a specified value.

Usage example (filter a key value, greater than a specified value) db. op_test.find ({"age": {$ gte: 19 }})

(4) $ lt: used to judge whether a key value is smaller than a specified value.

Usage example (filter a key value, less than a specified value) db. op_test.find ({"age": {$ lt: 20 }})

(5) $ lte: used to judge whether a key value is less than or equal to a specified value.

Usage example (filter a key value, less than or equal to a specified value) db. op_test.find ({"age": {$ lte: 20 }})

(6) $ ne: used to filter the value of a key without equivalent conditions.

Usage example (you can use $ ne to filter out a key that is not equal to a value) db. op_test.find ({"name" :{$ ne: "steven "}})

(7) $ in: used to specify the value of a key within the specified discrete value range

Usage example (filter whether the value of a key meets the condition, and the difference between $ or is that or can combine different conditions of different keys, while $ in is only a single condition) db. op_test.find ({"name": {$ in: ["steven", "jack"]})

(8) $ nin: opposite to $ in, used to specify that the key value does not exist in a specified discrete value range.

Usage example (opposite to $ in) db. op_test.find ({"name": {$ nin: ["steven", "jack"]})
  • Logic Operations

(1) $ or: Any combination of different query conditions (restrictions on any key can be met), as long as one of the conditions can be met.

Example: db. op_test.find ({"$ or": [{"name": "steven" },{ "age": 20}]})

(2) $ and: Compares with the $ or operator, and arbitrarily combines different query conditions (Restriction Conditions can be set for any key). All conditions must be met at the same time.

Example: db. op_test.find ({"$ and": [{"name": "steven" },{ "age": 20}]})

(3) $ not: a metadatabase statement, which must be combined with other conditions.

Usage example ($ not and $ lt operators are combined to return documents with age greater than or equal to 20): db. op_test.find ({"age": {"$ not": {"$ lt": 20 }}})

(4) $ nor: opposite to $ or, indicating that all conditions cannot be met, return.

Example: db. op_test.find ({"$ nor": [{"name": "steven" },{ "age": 20}]})
  • Element operations

(1) $ exists: Query documents that do not contain a certain attribute (key.

Usage instance (all documents containing the key name are returned) db. op_test.find ({"name": {"$ exists": true}) usage instance (all documents that do not contain the key name are returned) db. op_test.find ({"name": {"$ exists": false}) ps: The difference between true and false is to determine whether the key is included.

(2) $ type: filter data where a field is a BSON data type.

Usage example (return all documents whose name field is String type) db. op_test.find ({"name": {"$ type": 2}) ps: number after name for a specific query list see: http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type
  • Evaluation operations

(1) $ mod: return the remainder operator to filter documents whose results meet the criteria after regional operations.

Usage example (return the age value and the data whose result is 0 after 4 is obtained) db. op_test.find ({"age": {"$ mod": [4, 0]})

(2) $ regex: the document with the filtering value meeting the regular expression.

Usage example (return data whose name matches the specified regular expression, option limits the regular expression) db. op_test.find ({"name": {$ regex: "stev *", $ options: "I"}) ps: options see http://docs.mongodb.org/manual/reference/operator/query/regex/#op._S_regex

(3) $ text: full-text search and matching are implemented for fields with full-text indexes.

Usage example (search for the fields that construct the full-text index. The default value is English, and Chinese characters are not supported) db. op_test.find ({"$ text": {$ search: "steven", $ language: "en"}) ps: currently supported languages and abbreviations, see: http://docs.mongodb.org/manual/reference/text-search-languages/

(4) $ where: Powerful query keywords, but poor performance. You can pass in js expressions or js functions to filter data.

Usage example (return the document that meets the input js function, the function indicates that any field value in the document is "steven" will be returned) db. op_test.find ({"$ where": function () {for (var index in this) {if (this [index] = "steven") {return true ;}} return false ;}})
  • Array Operations

$ All: array query operator. The query condition is an array. The queried field is also an array, fields of the array type to be queried must be supersets of the query condition array (that is, large arrays must contain small arrays ).

Usage example: (query the array value corresponding to the key value, which must contain three elements: "a", "B", and "c") db. op_test.find ({"values": {$ all: ["a", "B"]})

$ ElemMatch: array query operator, used to specify that each element of the array meets the listed conditions at the same time. If not specified, the condition will be an or relationship.

Usage example: (to match the values array, there must be at least one element that meets all of the conditions) used to specify nested document operations, see: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/

$ Size: The quantity of values corresponding to keys of an array type meets the requirements.

Usage example: Filter documents that contain 3 array elements. Db. op_test.find ({"values": {$ size: 3 }})
  • Comments operations

$ Comment: During query, update, or other operations, you can add comments by adding the $ comment operator. Modified comments are recorded in logs for subsequent analysis.

Usage example db. collection. find ({<query>, $ comment: <comment> })
  • Geospatial operations

$ GeoWithin: This operator is based on 2d Spatial indexes. First, you must create a 2d Spatial index for a field in the document, and then use this operator, you can specify a multi-deformation parameter in a 2d space. The $ geoWithin operator is used to query the points contained in the multi-deformation range.

See: http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin

$ GeoIntersects: calculates the intersection of the current spatial range and the specified geo multi-Deformation Based on the 2d Spatial index.

See: http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/#op._S_geoIntersects

$ Near: specify a point based on the 2d Spatial index, and return all vertices with near and far points.

See: http://docs.mongodb.org/manual/reference/operator/query/near/#op._S_near

$ NearSphere: Based on 2d Spatial indexes, a vertex is specified to return all vertices from the nearest and far directions. Unlike the $ near operator, the distance calculation method $ nearSphere calculates the sphere distance. $ Near calculates the coordinate distance.

  • Projection Operations

$: If a value in the document is of the array type, you can use the $ operator to specify the projection of the array field, returns the first matched element in the array field, which is equivalent to truncating the entire array and returning only the first value.

Usage example: (returns the first element equal to "a" in the values array, that is, returns "a") db. op_test.find ({"values": {$ eq: "a" }}, {"values. $ ": 1}) returned results: {" _ id ": ObjectId (" 551117108cbfa0a55db5c5b9 ")," values ": [" a "]}

$ ElemMatch: This operator is involved in the preceding array operation. Another effect is that in the nested document application, the first qualified document in the array is returned, which can limit Multiple Combination conditions.

See: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch

$ Meta: used in combination with the text index of a full-text index. For an element with a full-text index, you can specify the change operator to return scores similar to query conditions. The higher the score, the higher the matching degree.

Usage example: db. op_test.find ({"$ text": {$ search: "steven", $ language: "en" }},{ score: {$ meta: "textScore "}}) execution result: {"_ id": ObjectId ("550fdba3c118f1b20bd51a9f"), "name": "steven", "age": 20, "score": 1.1}

$ Slice: the projection operation of an array type field to return a subset of the original data. For an array, there are several methods to return the subset:

Example: return the first 10 Comments of the blog. blog. find ({"comments": {"$ slice": 10}) usage example: return the last 10 comments of the blog to db. blog. find ({"comments": {"$ slice": 10}) usage example: Return to the blog to skip the first 10, then return 11th ~ 15 db. blog. find ({"comments": {"$ slice": [10, 5]})

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.