mongodb语法和mysql语法的区别_MySQL vs MongoDB 语法对比

MySQL语法

MongoDB语法

select * from t1;

db.t1.find({},{_id:0})

select id,name from t1;

db.t1.find({},{_id:0,id:1,name:1})

select id,name from t1 limit 1;

db.t2.findOne({},{_id:0,id:1,name:1})

select id,name from t1 limit 10;

db.t1.find({},{_id:0,id:1,name:1}).limit(10)

select id,name from t1 limit 10,4;

db.t1.find({},{_id:0,id:1,name:1}).skip(10).limit(4)

select id,name from t1 order by id

desc limit 10;

db.t1.find({},{_id:0,id:1,name:1}).limit(10).sort({id:-1})

select id,name from t1 where id=2;

db.t1.find({id:2},{_id:0,cid:1,name:1})

select id,name from t1 where i

d>=2;

db.t1.find({id:{$gte:2}},{_id:0,cid:1,name:1})

select id,name from t1 where i

d>=2 AND id <=10;

db.t1.find({id:{$gte:2,$lte:10}},{_id:0,cid:1,name:1})

select id,name from t1 where id in (1,3,5);

db.t1.find({id:{$in:[1,3,5]}},{_id:0,cid:1,name:1})

select id,name from t1 where i

d=1 AND name =’a’;

db.t1.find({$and:[{id:1},{name:"a"}]},{_id:0,cid:1,name:1})

select id,name from t1 where i

d=1 OR name =’a’;

db.t1.find({$or:[{id:1},{name:"a"}]},{_id:0,cid:1,name:1})

select count(*) from t1;

db.t1.find().count()

select distinct(name) from t1;

db.t1.distinct('name').count()

select count(*) from t1;

db.t1.aggregate({$group:{_id:null,count:{$sum:1}}})

select sum(cid) from t1;

db.t1.aggregate({$group:{_id:null,sum:{$sum:'$cid'}}})

select sum(cid) from t1 group by

name;

db.t1.aggregate({$group:{_id:'$name',sum:{$sum:'$cid'}}})

select count(*) from t1 where

name=’ABC’ group by class_id ;

db.t2.aggregate({$match:{name:"ABC"}},{$group:{_id:’class_id’,count:{$sum:1}}})

select count(*) from t1 group by

name having count(*) >2;

db.t1.aggregate({$group:{_id:'name',count:{$sum:1}}},{$match:{count:{$gt:2}}})

select id,name,count(*) from t1

group by id,name having

count(*) >=1;

db.t1.aggregate({$project:{id:1,"name":1}},{$match:{name:"bb"}},{$group:{"_id":{id:"$id",name:"$name"},count:{$sum:1}}}, {$match:{count:{$gte:1}}})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值