Nosql-MongoDB 2 advanced Query

Source: Internet
Author: User
Tags findone
ArticleDirectory
    • Find ()
    • Limit result set
    • Findone ()
    • Limit ()
    • Conditional operators <, <=,>, >= ,! =
    • $ Exists
    • Processing of null values
    • $ Mod
    • $ In & $ Nin
    • $ Not
    • $ Or
    • Array Operations
    • Count ()
    • Sort ()
    • Distinct ()
    • Skip ()
    • Query embedded documents
    • Use Regular Expressions
    • $ Where

Link

Http://www.cnblogs.com/zhaoyang/archive/2012/01/09/2317505.html

 

Connect ............

Advanced query find ()

1. query all records

DB. Users. Find ()

2. query records whose names are apple

DB. Users. Find ({"name": "Apple "})

3. query records whose country is China and whose gender is 1.

DB. Users. Find ({"country": "China", "Gender": 1 })

 

Limit result set

Corresponding to standard SQL, previously select * From, now need select name from...

1. the query name is apple, and only the name is displayed.

DB. Users. Find ({"name": "Apple" },{ "name": true })

 

Bytes ---------------------------------------------------------------------------------------------------------------------------

Note: The following parameter {Name: true} indicates that only the name is displayed. If one parameter is set to true, all other unspecified parameters are set to false by default, but the _ id field is set to true by default, if you do not want to display _ ID, you can write it like this:

DB. Users. Find ({"name": "Apple" },{ "name": True, "_ ID": false })

Bytes ---------------------------------------------------------------------------------------------------------------------------

 

2. query all records. Only the name and country records are displayed.

DB. Users. Find (null, {"name": True, "country": True, "_ ID": false })

Findone ()

As the name suggests, only the first query record is displayed, similar to select top 1...

DB. Users. findone ({"gender": 0 })

Limit ()

Similar to the top

DB. Users. Find ({"gender": 0}). Limit (2 );

Conditional operators <, <=,>, >= ,! =

$ Lt less than (less)

$ Less than or equal to (less than equals)

$ GT greater than (greater)

$ GTE greater than or equal to (greater than equals)

$ Ne is not equal to (not equals)

 

Find records with age greater than 30

DB. Users. Find ({"age": {$ GT: 30 }})

Records with age> = 30 and <= 40

DB. Users. Find ({"age": {$ GT: 30, $ LT: 40 }})

$ Exists

Used to determine whether a field exists. For example, if we add a record to the users set, only the name attribute exists.

DB. Users. insert ({Name: "testexists "})

 

Now we can find records that do not contain the age field.

DB. Users. Find ({"age": {$ exists: false }})

 

Records with age field but no gender field found

DB. Users. Find ({"age": {$ exists: true}, "Gender": {$ exists: false }})

Processing of null values

Differences between null and exists

Null indicates that the field is null or does not exist.

Exists indicates whether a field exists.

 

Compare the following two records

{"Name": NULL, "age": 44}

{"Age": 45}

 

If you use null to query DB. XXX. Find ({Name: NULL}), the above two records will be queried.

However, we only want to find the record with the name field empty.

DB. XXX. Find ({"name": {$ exists: True, $ in: [null]});

 

$ Mod

Modulo. For example, if we query a record whose age model is 10 or equal to 1, it should be 1, 11, 21 ,...

DB. Users. Find ({"age": {$ mod: []})

$ In & $ Nin

This is similar to the in and not in of standard SQL.

 

Query records whose age is 23 or 24

DB. Users. Find ({"age": {$ in: [23, 24]})

 

Query records whose age is not 23 or 24

DB. Users. Find ({"age": {$ Nin: [23, 24]})

$ Not

Can be used on any other conditions

DB. Users. Find ({$ not: {"age" :{$ GT: 25 }}})

$ Or

Query records whose age is 32 or whose name is zhangsan

DB. Users. Find ({$ or: [{"name": "zhangsan" },{ "age": 32}]})

Array Operations

Suppose we have the following two documents:

$ {"Name": "zhangsan", "booksnumber": [101,102,103]}

$ {"Name": "Lisi", "booksnumber": [101,103]}

$ All

DB. Users. Find ({"booksnumber" :{$ All: [101,102]});

Then we can find the first record, which must satisfy all the conditions in it.

$ Size

We need to query only two records in booksnumber.

DB. Users. Find ({"booksnumber" :{$ size: 2 }})

Count ()

Query the total number of records

DB. Users. Find (). Count ()

 

NOTE: If we use limit

DB. users. find (). limit (3 ). count () Here is the first three records, but the total number of records is still displayed after count () is called. If we need to return the number of records after the limit

DB. Users. Find (). Limit (3). Count (true)

Sort ()

This is also an operation as its name implies. Sort: 1 indicates Ascending Order-1 indicates descending order.

 

Sort by name in ascending order

DB. Users. Find (). Sort ({Name: 1 })

 

Sort by name in ascending order and then by age in descending order

DB. Users. Find (). Sort ({Name: 1, age:-1 })

Distinct ()

Query all names whose age is less than or equal to 50 and remove duplicates

DB. Users. Distinct ("name", {"Age": {$ LTE: 50 }})

Skip ()

This feature is usually used for paging

Skip three records and retrieve the following three records

DB. Users. Find (). Skip (3). Limit (3)

Query embedded documents

For example, to query such a record:

{"Books": {"bookid": 100, "bookname": "English "}}

 

DB. Users. Find ({"Books. bookid": 100 });

Use Regular Expressions

Using Regular Expressions is also a very powerful feature of MongoDB.

For example, we use a regular expression to filter names.

DB. Users. Find ({"name":/^ [Fc] {1 }/})

$ Where

You can use JavaScriptCodeQuery, which makes the query almost do anything.

 

DB. Users. Find ({$ where: function (){

// This indicates the current document that is traversed.

// Determine whether the document meets the find condition based on return true or false.

If (this. Name = "zhangsan "){

Return true;

}

For (VAR prop in this ){

If (this [prop] = "123 "){

Return true;

}

}

Return false;

}});

 

Next, we will introduce the cursor and stored procedure.

 

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.