Sort basic MongoDB queries

Source: Internet
Author: User

This article is summarized by the program yuan mm in our group

----------------------------------- I am a split line ------------------------------------------

Exact match

  • For a single key-Value Pair: {"Age": 28}, all documents whose "Age" value is 28 are returned.
  • Multiple key-value pairs: {"username": "Tom", "Age": 28} combine multiple query conditions, equivalent: condition 1 and condition 2 and... And condition n. This query document returns all documents whose username is Tom and their age is 28.

Condition match

Range
  • "$ Lt", "$ LTE", "$ GT", "$ GTE"
    Comparison operators:
    <    $lt <=   $lte>    $gt>=   $gte

    You can combine them to find values in a range.
    For example, the query documents between 18 and 35 are: {"Age": {"$ GTE": 18, "$ LTE": 35 }}
    This range query method is especially convenient for date type values.

  • "$ Ne"
    "Not equal" operator, corresponding :! =
    If the user name is not Tom, the query document is: {"username": {"$ ne": "Tom "}}

Or Query
  • $ In
    Queries multiple values corresponding to a key and performs or queries on a single key.
    For example, if the number of winning events is or 8, you need to find out the query documents for all the winning data: {"ticket_no": {"$ in": [1, 4, 8]}
  • $ Nin
    Correspondingly, $ Nin returns data that does not match the values in the array. For example, the query document for finding data that does not win the prize is: {"ticket_no": {"$ Nin": [1, 4, 8]}.
  • $ Or
    It is more common to query any value of multiple keys. It accepts an array of all possible conditions as a parameter, or it can contain other conditions. For example:
    {     "$or": [        {            "ticket_no":{                "$in":[1, 4, 8]            }        },         {            "winner":true        }    ]}

    The first condition of $ or should be matched to more documents as much as possible, so as to be the most effective.

$ Not

$ Not is a metaconditional sentence. It can be used on any other condition and the table is reversed.
For example: Query document: {"value": {"$ MOD": [5, 1]}. The values that meet the criteria include: 1, 6, and 11.
To search for data with values of 2, 3, 4, 5, 7, 8, 9, 10, and 12, you can use: {"value": {"$ not ": {"$ MOD": [5, 1]}.

$ Exists

Used to query whether a key exists in the document. For example, find the document with the key name key1: {"key1": {"$ exists": false} that does not exist }};
Conversely, {"key1": {"$ exists": true} indicates that the key1 key exists.

Type match

Null

Query the document {"X": NULL}. After the statement is executed, it returns the document containing the key-Value Pair "X": NULL, and the document without the X key.

Regular Expression

All the regular expressions supported by PCRE can be accepted by MongoDB.
If the query document {key1 "} is returned.
It can be used in like scenarios in SQL.

Query Arrays

Each element in the array is the value of the entire key. If any:

Document 1: {"Fruit": ["apple", "Pear", "Peach"]}, document 2: {"Fruit": ["Peach", "banana ", "Apple"]}, document 3: {"Fruit": ["orange", "banana", "apple"]},
  • Single element matching
    If the query document is {"Fruit": "apple"}, documents 1, 2, and 3 are matched successfully.
  • Match Multiple Elements
    The $ all clause is required. If the query document is {"Fruit": {"$ all": ["apple", "Peach"]}, the document 1 and 2 will be matched and irrelevant to the element sequence.
  • Exact match
    If the query document is: {"Fruit": ["apple", "Pear", "Peach"]}, only document 1 is matched. for missing or redundant documents, and those with different orders won't match.
  • Subscript matching
    The key. index method is used, and the array subscript starts from 0. For example, if you query the document {"fruit.2": "apple"}, the document 2 and 3 will be matched.
  • Length matching
    If the query document is: {"Fruit": {"$ size": 3 }}, it indicates an array with a length of 3. the query documents 1, 2, and 3 will be matched.

Query of embedded documents
  • Point representation
    For example:
    {    "_id":1,    "name":{"first":"Joe", "last":"Smith"}}

    You can query: {"name. First": "Joe", "name. Last": "Smith "}

  • $ Elemmatch
    It is used when multiple key operations are required for an embedded document.
    If any:
    {  "comments": [    {      "name": "Tom",      "score": 3,      "comment": "bad"     },    {      "name": "Jim",      "score": 6,      "comment": "good"     }  ]}

    To search for comments whose Tom scores are greater than 5, you can only: {"Comments": {"$ elemmatch": {"name": "Tom", "score ": {"$ gt": 5 }}}}
    But it cannot: {"Comments": {"name": "Tom", "score": {"$ gt": 5 }}. it cannot match the "comment" key.
    Or {"comments. Name": "Tom", "comments. Score": {"$ gt": 5}, not the same comment.

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.