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 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 users where name like "%Joe%" |
db.users.find({name:/Joe/}) |
select * from users where name LIKE "Joe%" |
db.users.find({name:/^Joe/}) |
select * from users where age>33 and age<=40 |
db.users.find({'age':{$gt:33,$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 10 skip 20 |
db.users.find().limit(10).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 users where age > 30 |
db.users.find({age: {'$gt': 30}}).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'});
|