MongoDB vs. SQL Common statement Comparison

Source: Internet
Author: User
Tags findone

Inert into users value (3,5)

Db.users.insert ({a:3,b:5})

Select a, b from users

Db.users.find ({}, {a:1,b:1})

SELECT * from Users

Db.users.find ()

SELECT * from Users where age=33

Db.users.find ({age:33})

Select a, b from users where age=33

Db.users.find ({age:33}, {a:1,b:1})

SELECT * from the users where age=33 order by name

Db.users.find ({age:33}). Sort ({name:1})

SELECT * from Users where age>33

Db.users.find ({age:{$gt: 33}})

SELECT * from Users where age!=33

Db.users.find ({age:{$ne: 33}})

SELECT * from the users where name like "%joe%"

Db.users.find ({name:/joe/})

SELECT * from the users where name like "joe%"

Db.users.find ({name:/^joe/})

SELECT * from users where age>33 and age<=40

Db.users.find ({' age ': {$gt:, $lte: 40}})

SELECT * from Users ORDER BY name Desc

Db.users.find (). Sort ({name:-1})

SELECT * from users where a=1 and b= ' Q '

Db.users.find ({a:1,b: ' Q '})

SELECT * from users limit skip 20

Db.users.find (). Limit (Ten). Skip (20)

SELECT * from users where a=1 or b=2

Db.users.find ({$or: [{a:1}, {b:2}]})

SELECT * from users limit 1

Db.users.findOne ()

Select order_id from Orders o, Order_line_items Li where li.order_id=o.order_id and li.sku=12345

Db.orders.find ({"Items.sku": 12345},{_id:1})

Select Customer.name from Customers,orders where orders.id= "q179" and Orders.custid=customer.id

var o = Db.orders.findOne ({_id: "q179"});

var name = Db.customers.findOne ({_id:o.custid})

Select DISTINCT last_name from users

Db.users.distinct (' last_name ')

Select COUNT (*y)

From users

Db.users.count ()

Select COUNT (*y)

From the users where age > 30

Db.users.find ({age: {' $GT ': $}}). Count ()

Select count (age) from users

Db.users.find ({age: {' $exists ': true}}). Count ()

Create index Myindexname on users (name)

Db.users.ensureIndex ({name:1})

Create index Myindexname on users (name,ts desc)

Db.users.ensureIndex ({name:1,ts:-1})

Explain select * from users where z=3

Db.users.find ({Z:3}). Explain ()

Update users set a=1 where b= ' Q '

Db.users.update ({b: ' Q '}, {$set: {a:1}}, False, True)

Update users set a=a+2 where b= ' Q '

Db.users.update ({b: ' Q '}, {$inc: {a:2}}, False, True)

Delete from users where z= "abc"

Db.users.remove ({z: ' abc '});

MongoDB vs. SQL Common statement Comparison

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.