MongoDB Syntax Backup

Source: Internet
Author: User

MongoDB Syntax

MongoDB benefits Quite a lot, such as multi-column index, query can use some statistical functions, support multi-conditional query, but the current multi-table query is not supported, you can find a way to solve the problem of multi-table query through data redundancy.

Query Colls all data

Db.colls.find ()//select * from Colls

Querying by specifying criteria

Db.colls.find ({' last_name ': ' Smith '}),//select * from Colls where last_name= ' Smith '

Specify a multi-criteria query

Db.colls.find ({x:3, y: "foo"}),//select * from Colls where x=3 and y= ' foo '

Specify criteria range Query

Db.colls.find ({j: {$ne: 3}, K: {$gt: ten}});//select * from Colls where j!=3 and k>10

Query does not include a content

Db.colls.find ({}, {a:0});//query all data except for a of 0

Support <, <=, >= query, with symbolic substitution for $lt, $lte, $GT, $gte

Db.colls.find ({"field": {$gt: Value}});

Db.colls.find ({"field": {$lt: Value}});

Db.colls.find ({"field": {$gte: Value}});

Db.colls.find ({"field": {$lte: Value}});

You can also make a range query for a field

Db.colls.find ({"field": {$gt: value1, $lt: value2}});

Not equal to query character $ne

Db.colls.find ({x: {$ne: 3}});

In query with character $in

Db.colls.find ({"field": {$in: Array}});

Db.colls.find ({j:{$in: [2,4,6]}});

Not in query with character $nin

Db.colls.find ({j:{$nin: [2,4,6]}});

Character $mod for modulo query

Db.colls.find ({A: {$mod: [ten, 1]}})//where a% 10 = = 1

$all Query

Db.colls.find ({A: {$all: [2, 3]}});//Specifies that a satisfies any value in the array

$size Query

Db.colls.find ({A: {$size: 1});//The number of objects queried, this query queries the number of child objects of a 1 record

$exists Query

Db.colls.find ({A: {$exists: true}}); There is data for the A object

Db.colls.find ({A: {$exists: false}}); There is no data for the A object

$type Query the type value of the $type value to bsonhttp://bsonspec.org/data

Db.colls.find ({A: {$type: 2}}); Match A to string type data

Db.colls.find ({A: {$type: 16}}); Match A to int type data

Using regular expression matching

Db.colls.find ({name:/acme.*corp/i});//similar to SQL

Inline Object Query

Db.colls.find ({"Author.name": "Joe"});

1.3.3 versions and later versions include $not queries

Db.colls.find ({name: {$not:/acme.*corp/i}});

Db.colls.find ({A: {$not: {$mod: [10, 1]}}});

Sort () sorting

Db.colls.find (). Sort ({ts:-1});//1 to ascending 2 to descending

Limit () returns the number of restricted query data

Db.colls.find (). Limit (10)

Skip () skips some data

Db.colls.find (). Skip (10)

Snapshot () snapshot guarantees no duplicate data return or object loss

Count () counts the number of query objects

Db.students.find ({' address.state ': ' CA '}). Count ();//High efficiency

Db.students.find ({' address.state ': ' CA '}). ToArray (). length;//is inefficient

Group () resembles the group by function in SQL by grouping query results

Distinct () returns non-repeating values

MongoDB syntax Backup (RPM)

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.