MongoDB and MySQL operations, mongodbmysql

Source: Internet
Author: User
Tags mongodb query

MongoDB and MySQL operations, mongodbmysql

On the left is the mongodb query statement, and on the right is the SQL statement. It is easy to use.

Db. users. find () select * from users

Db. users. find ({"age": 27}) select * from users where age = 27

Db. users. find ({"username": "joe", "age": 27}) select * from users where "username" = "joe" and age = 27

Db. users. find ({}, {"username": 1, "email": 1}) select username, email from users

Db. users. find ({}, {"username": 1, "_ id": 0}) // no case // Column Filtering is added immediately, and _ id is returned; must explicitly block _ id returned

Db. users. find ({"age": {"$ gte": 18, "$ lte": 30 }}) select * from users where age> = 18 and age <= 30 // $ lt (<) $ lte (<=) $ gt (>) $ gte (> =)

Db. users. find ({"username": {"$ ne": "joe"}) select * from users where username <> "joe"

Db. users. find ({"ticket_no": {"$ in": [725,542,390]}) select * from users where ticket_no in (725,542,390)

Db. users. find ({"ticket_no": {"$ nin": [725,542,390]}) select * from users where ticket_no not in (725,542,390)

Db. users. find ({"$ or": [{"ticket_no": 725 },{ "winner": true}]}) select * form users where ticket_no = 725 or winner = true

Db. users. find ({"id_num": {"$ mod": [5, 1]}) select * from users where (id_num mod 5) = 1

Db. users. find ({"$ not": {"age": 27}) select * from users where not (age = 27)

Db. users. find ({"username": {"$ in": [null], "$ exists": true }}) select * from users where username is null // If you directly query through find ({"username": null}), the records with "no username" are filtered out together.

Db. users. find ({"name":/joey? /I}) // regular query. value is a PCRE expression.

Db. food. find ({fruit :{$ all: ["apple", "banana"] }}) // query the array. The fruit field contains "apple ", it also contains "banana" records

Db. food. find ({"fruit.2": "peach"}) // queries the array. In the fruit field, 3rd (starting from 0) elements are recorded by peach.

Db. food. find ({"fruit": {"$ size": 3}) // query the array. The number of elements in the array is 3 Records, $ size cannot be used together with other operators

Db. users. findOne (criteria, {"comments": {"$ slice": 10}) // queries an array and returns only the top 10 comments in the array, you can also {"$ slice":-10}, {"$ slice": [23, 10]}; return the last 10, and the middle 10, respectively.

Db. people. find ({"name. first": "Joe", "name. last": "Schmoe"}) // nested Query

Db. blog. find ({"comments": {"$ elemMatch": {"author": "joe", "score": {"$ gte": 5 }}}}) // nested query, used only when the nested element is an array,

Db. foo. find ({"$ where": "this. x + this. y = 10 "}) // complex query, $ where is of course very convenient, but inefficient. For complex queries, the order should be regular-> MapReduce-> $ where

Db. foo. find ({"$ where": "function () {return this. x + this. y = 10;} "}) // $ where supports javascript Functions as query Conditions

Db. foo. find (). sort ({"x": 1 }). limit (1 ). skip (10); // return the number (10, 11], sorted by "x"; the order of the three limit is arbitrary, avoid using large-number in the skip

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.