MongoDB 基本语法

1、数据准备

1、MongoDB数据,工具为:Robo 3T

MongoDB  基本语法

 mongo-data

2、MySQL数据

MongoDB  基本语法

 MySQL

2、新增语句

方法说明语法备注
新增MongoDB

db.getCollection(‘user‘).insert({"userId" : "014","uclass" : "B","name" : "Back","age" : 11,"email" : "","birthday" : ISODate("2018-07-31T03:46:13.885Z"),"dataStatus" : 1});

 
MySQLINSERT INTO `sz-temp`.`user` (`userId`, `uclass`, `name`, `age`, `email`, `birthday`, `dataStatus`) VALUES (‘014‘, ‘B‘, ‘Back13‘, ‘20‘, ‘‘, ‘2013-07-31 11:46:13‘, ‘0‘); 

3、删除语句

方法说明语法备注
删除MongoDBdb.getCollection(‘user‘).remove({"userId":"014"}); 
MySQLdelete from user where userId = ‘014‘; 

4、修改语句

方法说明语法备注
修改MongoDBdb.getCollection(‘user‘).update({"userId":"013"}, {$set:{"email":"", "age":20}}); 
MySQLupdate user set email = ‘‘, age = 20 where userId = ‘013‘; 

5、查询语句

查询方法说明语法备注
查询所有MongoDBdb.getCollection(‘user‘).find({}); 
MySQLselect * from user; 
查询条件:=MongoDBdb.getCollection(‘user‘).find({"uclass":"A"}); 
MySQLselect * from user where uclass = ‘A‘; 
查询条件:likeMongoDBselect * from user where name like ‘%Ba%‘; 
MySQLdb.getCollection(‘user‘).find({"name":/Ba/}); 
查询条件:distinctMongoDBselect distinct uclass from user u; 
MySQLdb.getCollection(‘user‘).distinct("name"); 
查询条件:$gtMongoDBdb.getCollection(‘user‘).find({"age":{$gt:16}});greater than  >
MySQLselect * from user where age >16; 
查询条件:$gteMongoDBdb.getCollection(‘user‘).find({"age":{$gte:16}});gt equal  >=
MySQLselect * from user where age >= 16; 
查询条件:$ltMongoDBdb.getCollection(‘user‘).find({"age":{$lt:16}});less than  <
MySQLselect * from user where age < 16; 
查询条件:$lteMongoDBdb.getCollection(‘user‘).find({"age":{$lte:16}});lt equal  <=
MySQLselect * from user where age <= 16; 
查询条件:$neMongoDBdb.getCollection(‘user‘).find({"age":{$ne:16}});not equal  !=
MySQLselect * from user where age != 16; 
查询条件:$eqMongoDBdb.getCollection(‘user‘).find({"age":{$eq:16}});等效于:db.getCollection(‘user‘).find({"age":16});equal  =
MySQLselect * from user where age = 16; 
查询条件:inMongoDBdb.getCollection(‘user‘).find({"uclass":{$in:[‘A‘, ‘B‘]}}); 
MySQLselect * from user where uclass in (‘A‘, ‘B‘); 
查询条件:andMongoDBdb.getCollection(‘user‘).find({"uclass":"B", "age":{$gt:16}}); 
MySQLselect * from user where uclass = ‘B‘ and age > 16; 
查询条件:orMongoDBdb.getCollection(‘user‘).find({$or:[{"uclass":"A"},{"class":"B"}]}); 
MySQLselect * from user where uclass = ‘A‘ or  uclass = ‘B‘; 
查询条件:时间MongoDBdb.getCollection(‘user‘).find({"birthday":{$gt: new Date("2008-08-14T06:24:40.110Z"), $lt: new Date("2015-08-14T06:14:40.089Z")}}); 
MySQLselect * from user where birthday > ‘2008-08-14 06:24:40‘ and birthday < ‘2015-08-14 06:14:40‘; 
查询条数:countMongoDBdb.getCollection(‘user‘).find({"uclass":"A"}).count(); 
MySQLselect count(1) from user where uclass = ‘A‘; 
查询条件:sort升序MongoDBdb.getCollection(‘user‘).find({}).sort({"age":1}); 
MySQLselect * from user order by age asc; 
查询条件:sort降序MongoDBdb.getCollection(‘user‘).find({}).sort({"age":-1}); 
MySQLselect * from user order by age desc; 
聚合查询:count单列MongoDBdb.getCollection(‘user‘).aggregate([{$group:{_id:"$uclass",num:{$sum:1}}}]); 
MySQLselect uclass, count(1) as num from user group by uclass; 
聚合查询:count多列MongoDBdb.getCollection(‘user‘).aggregate([{$group:{_id:{uclass:"$uclass", age:"$age"},num:{$sum:1}}}]); 
MySQLselect uclass, age, count(1) as num from user group by uclass, age; 
分页查询:limit nMongoDBdb.getCollection(‘user‘).find({}).limit(5);查询前n条
MySQLselect * from user limit 5; 
分页查询:limit m,nMongoDBdb.getCollection(‘user‘).find({}).limit(5).skip(5);查询n条,从第m条开始
MySQLselect * from user limit 5,5; 
查询指定字段MongoDBdb.getCollection(‘user‘).find({}, {userId:1, name:1});第一个{}为查询条件
MySQLselect userId, name from user; 
排查指定字段MongoDBdb.getCollection(‘user‘).find({}, {dataStatus:0, _id:0});第一个{}为查询条件
MySQL 

6、参考网站

本文摘自  https://www.cnblogs.com/mao2080/p/9570909.html

http://www.runoob.com/mongodb/mongodb-aggregate.html

https://www.jianshu.com/p/5e870132ca7c

相关推荐