MongoDB Learning Notes < a >

Source: Internet
Author: User
Tags findone

Key points and use of 1.find functions

Introduction and use of find

MongoDB data queries use the Find function, which functions the same as the Select function in SQL, providing many features similar to the relational database, including mapping, sorting, and so on.

Db. Collection name. Find (Query,fields,limit,skip)

There are four main parameters:

Query: Indicates the criteria for the search, equivalent to the WHERE statement in SQL

Fields: For field mappings, specifying whether to return the field, 0 for not returning, 1 for returning

Limit: Limits the number of documents for a query's result set, specifying that the query returns the upper bound of the result set

Skip: Skips the result of a certain amount of data, sets the offset of the first returned document

Precautions

MongoDB does not support a connection query between multiple collections, the Find function can only be queried for one collection at a time

Returns all documents in the collection when the find parameter is empty or if the query condition is an empty document

In addition to limit and skip as arguments to the Find function, you can also use the limit and skip functions to decorate query results separately

The returned query result set is unordered by default, and if you need to sort the result set, you can use the Sort function

Db.collection.findOne () returns only the first data

Indexes can be created to speed up the query speed of a query when the number of collections documents is large

In addition to using the Find function to implement basic queries, MongoDB also provides an aggregation framework for complex queries

2. Introduction and use of query operators

2.1-piece operator

$GT >

$gte >=

$lt <

$lte <=

$ne! =

Such as: A set B set of Chinese documents have a property x value is an integer, you need to find 10<x<=30 document

Db. B.find ({"x": {"$GT": Ten, "$lte": 30}})

For example, look up a date attribute from a collection b the day value is greater than the 2012/01/01 document data db. B.find ({"Day": {"$GT": New Date ("2012/01/01")}})

2.2 $in include/$nin not included

$in: Query the document that matches the specified condition value;

$nin: The query does not match the document for the specified condition value;

SQL: Syntax: field in (' Value 1 ', ' value 1 ' ...)

Mongodb:db. B.find ({"x": {"$in": [' value 1 ', ' Value 2 ',.....]}})

SQL: Syntax: Field not IN (' Value 1 ', ' value 1 ' ...)

Mongodb:db. B.find ({"x": {"$nin": [' value 1 ', ' Value 2 ',.....]}})

$in/$nin Advantages: You can specify different types of conditions and values.

2.3 $or or query

$or: Query a document that matches multiple values of multiple criteria;

SQL: syntax: Field 1 = ' xxx ' or field 2 in (' xxx ') .....

Mongodb:db. B.find ({"$or": [{"x": {"$in": [' value 1 ', ' Value 2 ' ...]}},{"y": "3"}]})

2.4 $all matches All

{"Name": Jack, "age": [All-in-all]}

{"Name": Jack, "age": [1,4,3]}

Db. B.find ({"Age": {"$all": [2,3]}})

Result: {"name": Jack, "age": [All-in-all]}

$all query pattern is more like the intersection, that is, to satisfy the query conditions to be queried.

2.5 $exists determine if a property exists

Db. B.find ({"name": {"$exists": true})--Find the document where the attribute name exists

Db. B.find ({"name": {"$exists": false}})--Find a document with a property name that does not exist

2.6 Case where the property value is null

Here are some things to know:

> DB. C.find ()

{"_id": ObjectId ("5018fccd1781352fe25bf511"), "a": "+", "B": "14"}

{"_id": ObjectId ("5018fccd1781352fe25bf512"), "a": "" "," B ":" 15 "}

{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}

> DB. C.find ({"c": null})

{"_id": ObjectId ("5018fccd1781352fe25bf511"), "a": "+", "B": "14"}

{"_id": ObjectId ("5018fccd1781352fe25bf512"), "a": "" "," B ":" 15 "}

{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}

The Visible query property c value is a null document, including the attribute C value is null, and the attribute C does not exist in two parts. If you want to query only documents with property C as null

As follows:

> DB. C.find ({"C": {"$in": [null], "$exists": True}})

{"_id": ObjectId ("5018fccd1781352fe25bf510"), "a": "+", "B": "+", "C": null}

2.7 $not element Conditional sentence

Can be used in conjunction with other conditions, i.e. documents that are not within the matching range

2.8 $mod modulo operation

Db. B.find ({"Age": {"$mod": [5,1]}})--represents all documents looking for ages/5 + 1

If you look for all documents that are older than 5 + 1, combine $not operations:

Db. B.find ({"Age": {"$not": {"$mod": [5,1]}}})

2.9 Regular Expressions

Db. B.find ({"Name":/jack/i})

2.10 $size

{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [1, 2, 3]}

{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}

> DB. C.find ({"B": {"$size": 2}})

{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}

2.11 $slice

Returns a subset of the array, that is, how many bars (ranges) are returned based on a property. You can also accept the offset value and the number of elements to return to return the intermediate result.

> DB. C.find ()

{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [1, 2, 3]}

{"_id": ObjectId ("501e71607d4bd700257d8a42"), "a": "1", "B": [1, 2]}

> DB. C.findone ({},{"B": {"$slice": [2,3]}})

{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [3]}

> DB. C.findone ({},{"B": {"$slice":-2}})

{"_id": ObjectId ("501e71557d4bd700257d8a41"), "a": "1", "B": [2, 3]}

2.12 $where

You can execute the task JavaScript as part of the query.

The value of the $where can be a function, a string, and so on.

Db. C.find ({"$where": function () {return THIS.A = = "1"}}) and DB. C.find ({"$where": "THIS.A = = ' 1 '"}})

Note: Queries with $where clauses are much slower than regular queries in speed. Because the document needs to be converted from Bson to a JavaScript object, it is then run through an expression of "$where".

Do not use indexes. Regular queries can be used to do pre-filtering, configure the "$where" query for tuning, can achieve without sacrificing performance requirements.

3. Cursors

using cursors to return the results of find execution, the client's implementation of the cursor can often limit the number of results, skip partial results, sort, and more effectively control.

var cursor = db. C.find ()--defining cursors

while (Cursor.hasnext ()) {

var obj = Cursor.next ();

Print (OBJ.A);

......

}

Db. C.find (). Limit (10)--Limit the number of results in a query to 10 bars

Db. C.find (). Skip (10)--ignores the first 10 matches and displays all documents starting with the 11th matching document

Db. C.find (). Sort ({"A": -1})--sort with a key/value that is sorted by an attribute, 1: Ascending, 1: Descending

Advanced query Options:

$maxscan: integer--Specifies the maximum number of documents scanned by the query

$min:d ocument--Start condition of the query

$max:d ocument--the end condition of the query

$hint:d ocument--Specify which index the server uses to query

$explain: boolean-Gets the details of the query execution (index used, number of results, time-consuming, etc.) instead of actually executing the query

$snapshot: boolean-ensures that the result of the query is a consistent snapshot at the moment the query executes

MongoDB Learning Notes < a >

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.