MongoDB Learning Notes-Query

Source: Internet
Author: User
Tags findone

Use the Find or FindOne function to execute a query in MongoDB

Find function

Db.c.find ()--query collection C all

Db.c.find ({"Name": "Zhangsan"})

Note: The value of a query condition must be a constant, that is, you cannot use a variable

Db.c.find ({"Name": THIS.name})//This won't work.

Query criteria

"$lt" Correspondence <

"$lte" corresponds to <=

"$GT" Correspondence >

"$gte" corresponds to >=

"$ne" corresponds to! =

Usage:

Db.users.find ({"Age": {"$gte": +, "$lte": 30}})//Find users older than or equal to 18 less than or equal to 30

Db.users.find ({"name": {"$ne": "Zhangquan Egg"}})

or query

MongoDB in two ways to implement or query:

The "$in" can be used to query multiple values of a key, which is flexible enough to specify different types of conditions

"$nin" is opposite to "$in" action

"$or" can query for any given value in more than one base

Usage:

Db.users.find ({"name": {"$in" ["Wang Nima", "Zhangquan Egg"]}})

Db.users.find ("user_id": {"$in": [123456, "123456"]})//lookup when the data type of the user ID key is changed

Db.users.find ({"name": {"$or": ["Age": {"$in": [18,20,25]},{"Gender": 1}]}})

$not

$not is a meta-conditional operator that can be used on top of any other condition.

Db.users.find ({"Id_num": {"$not": {"$mod": [5,1]}})//"$mod" is the modulo operator

Null type

The Users collection has three data as follows:

{"Name": Wang Nima "," Age ": 18}

{"Name": "Zhangquan Egg", "Age": 20}

{"Name": "Tangma", "Age": null}

Where the age of "Tangmaru" is a null value

Then query: Db.users.find ({"Age": null})

The result is not only {"name": "Tangma", "Age": null}, the other two will be detected. Because NULL will not only match a document with a key value of NULL, but also

Matches a document that does not contain this key.

If you only want to match a document with a key value of NULL, you need to use "$exists"

Db.users.find ({"Age": [null], "$exists": true})

Regular expressions

Db.users.find ({"name":/Zhang./})

Querying arrays

Db.food.insert ({"Fruit": ["apple", "banana", "Peach"]})

Inquire:

Db.food.find ({"Fruit": "Banana"})

The result will match the document inserted above

$all: Used to find arrays that match multiple conditions

For example, a common collection that contains 3 elements:

Db.food.insert ({"Fruit": ["apple", "banana", "Peach"]})

Db.food.insert ({"Fruit": ["apple", "kumquat", "Orange"]})

Db.food.insert ({"Fruit": ["Cherry", "banana", "Apple"]})

To find documents that have both "apple" and "banana":

Db.food.find ({fruit:{$all: ["Apple", "Banana"]}})

The results will find the 1th and 3rd documents

$size: An array for querying a specific length

Db.food.find ({"Fruit": {"$size": 3}})

Match by index: Db.food.find ({"fruit.2": "Peach"]})

$slice: The result set used to split the Find/findone return value, returning a subset of the array elements that a key matches.

Db.food.findOne ({},{"fruit": {"$slice": 2}})

Db.food.findOne ({},{"fruit": {"$slice":-2}})//Right-to-left

$elemMatch: Requires MongoDB to use query conditions simultaneously on the same array (by default multiple conditions are applied to multiple arrays)

Db.test.find ({"x": {"$elemMatch": {"$GT": Ten, "$lt": 20}})

$where Query

$where used for any query, it can execute arbitrary JavaScript. Therefore, for safety reasons, $where should be strictly or not used.

Db.food.find ({"$where": function () {Forvar current in this () {    //todo}}})

Using $where queries is much slower than regular queries because it converts each document from a Bson object to a JavaScript object.

Limit, skip, and sort

Limit function: For limiting the number of query results

Db.food.find (). Limit (3)//returns only 3 matching results. If the result of the match is less than 3, the result of the matching quantity is returned.

Skip function: Used to skip a specified number of documents

Db.food.find (). Skip (1)//Skip 1th document, return the second two

Sort function: Sorts the results of the query, 1 is ascending,-1 is descending

Db.food.find (). Sort ({"Fruit": 1})

MongoDB Learning Notes-Query

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.