MongoDB query usage, notes to prevent forgetting

Source: Internet
Author: User
Tags mongodb query

Recently in using this thing, to prevent forgetting, write it down.

Collection Simple Query method

MongoDB syntax: Db.collection.find ()//collection is the name of the collection, which can be created on its own.

Compare SQL statements: SELECT * FROM collection;

Queries all the documents in the collection, that is, all the data in the query table in the relational database.

Returns the established key value

MongoDB Syntax: db.collection.find ({},{"userid": 1})

Compare SQL statements: select UserID from collection;

Conditional filtering

MongoDB Syntax: Db.collection.find ({"userid": 495})

Compare SQL statements: SELECT * from Collectionwhere userid = 495;

  

Query full format writing explanation

Db.collection.find ({},{})

The first {}, which is written equal to the condition after where in the SQL statement, the multi-conditional format {"Key": Value, "Key2": "Value2"}

In the second {}, the write is equivalent to the specific field in the SQL statement that follows the select, in the format {"Key": Value, "Key2": Value}

This field is displayed when value = 0 o'clock is not displayed for this field and when value!=0 equals any value other than 0.

Cases:

MongoDB query:

Db.error.find ({"userid": 1, "type": "Debug"},{"userid": 1, "type": 1, "Myssage": 1})

SQL query:

Select Userid,type,message from error where userid=1 and type = "Debug";

Sort sorts and limit returns the number of fixed entries

MongoDB query:

Db.collection.find ({"userid": 1, "type": "Debug"},{"userid": 1, "type": 1, "Myssage": 1}). Sort ("Time":-1). Limit (10)

SQL query:

Select Userid,type,message from collection where userid=1 and type = "Debug" ORDER by time DESC limit 10;

Count returns the total number of result sets

MongoDB query:

Db.collection.count ()

SQL query:

Select COUNT (*) from collection;

Query Operator "$GT"--greater than operator

MongoDB query:

Db.collection.find ({"userid": {"$gt": "494"}})

SQL query:

SELECT * FROM collection where UserID > 494;

Query Operator "$gte"-greater than or equal to

MongoDB query:

Db.collection.find ({"userid": {"$gte": "494"}})

SQL query:

SELECT * FROM collection where UserID >= 494;

Query Operator "$LT"-less than

MongoDB query:

Db.collection.find ({"userid": {"$lt": "494"}})

SQL query:

SELECT * FROM collection where UserID <494;

Query Operator "$lte"-less than or equal to

MongoDB query:

Db.collection.find ({"userid": {"$lte": "494"}})

SQL query:

SELECT * FROM collection where UserID < = 494;

The query Operator "$ne" is not equal to

MongoDB query:

Db.collection.find ({"userid": {"$ne": "494"}})

SQL query:

SELECT * FROM collection where userid! = 494;

Query operator "NULL query"--null

MongoDB query:

Db.collection.find ({"userid": null})

SQL query:

SELECT * FROM collection where userid is null;

The query Operator "$all" and matches all

MongoDB query:

Db.collection.find ({"userid": {"$all": "494"}})

SQL query:

SELECT * FROM collection where UserID = 494;

When the document type is an array, the $all is used to match, and the non-array type is used as a single match.


Query Operator "$size" for array queries, queries for arrays of specified lengths

MongoDB query:

Db.collection.find ({"Remark": {"$size": "3"}})

Query Operator "$in"-in range

MongoDB query:

Db.collection.find ({"userid": {"$in": ["297", "495"]}})

SQL query:

SELECT * FROM collection where UserID in (297,495);


Query Operator "$nin"-not in range

MongoDB query:

Db.collection.find ({"userid": {"$nin": ["297", "495"]}})

SQL query:

SELECT * FROM collection where UserID is not in (297,495);


The query Operator "$and" and contains at least two expressions, and two expressions satisfy the return of a document

MongoDB query:

Db.collection.find ({"$and": [{"userid": "495"},{"type": "Info"}]})

SQL query:

SELECT * FROM collection where userid=495 and type= ' info ';


Query Operator "$nor" with at least two expressions and two expressions that are not satisfied with the return of a document

MongoDB query:

Db.collection.find ({"$nor": [{"userid": "495"},{"userid": "297"}]})

SQL query:

SELECT * FROM collection where UserID is not in (297,495);


Query Operator "$not"-finds documents that do not match expressions, cannot be used alone, and must be used in conjunction with other expressions

MongoDB query:

Db.collection.find ({"userid": {"$not": {"$gt": "297"}})

Equivalent to: Db.collection.find ({"userid": {"$lte": "297"}})

SQL query:

SELECT * FROM collection where UserID <=297;



The query Operator "$or"- contains at least two expressions, and two expressions satisfy at least one document return

MongoDB query:

Db.collection.find ({"$or": [{"userid": "495"},{"userid": "297"}]})

SQL query:

SELECT * FROM collection where UserID =297 or UserID = 495;


Query Operator "$exists"--Query the document for the existence of a field

MongoDB query:

Db.collection.find ({"$exists": "True"})


Query Operator "$mod"--key value pair variable parameter modulo, value equals another parameter

MongoDB query:

Db.collection.find ({"userid": {"$mod": [10,7]}})

Execution condition: The UserID field value must be a number, userid to 10 modulo, the value equals 7 of the document returns.

SQL query:

SELECT * FROM collection where (user_id%10) =7


Query Operator "$regex"--Regular match

MongoDB query:

Db.collection.find ({"userid":/5$/})

SQL query:

SELECT * FROM collection where UserID like '%5 ';

SQL regular notation:             SELECT * FROM collection where UserID regexp ". 5$";  The last digit of the regular matching userid is 5, and SQL only uses RegExp to use complex regular expressions, so you can't do complex regular matches by using the like method.

Query Operator "$slice"-controls the number of elements in the returned array element

MongoDB query:

Db.collection.find ({},{"remark": {"$slice": 5})

Remark array key values, returning the first 5 elements in the array key value

Db.collection.find ({},{"remark": {"$slice": [10,5]})

Remark the array key value, returns the 11th to 15th element in the array key value, the offset is 10, and then returns 5.

Db.collection.find ({},{"remark": {"$slice":-5})

Remark array key value, returns the first 5 elements in the array key value

Gradually add ...

MongoDB query usage, notes to prevent forgetting

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.