1、mongo的基础指令
show dbs 获取你当前所有的数据库
use dataBase_name 创建数据库(没有-创建/存在-使用)
db 指查询你当前的数据库
db.stats() 查询你当前数据库的状态
db.dropDatabase() 删除你当前的数据库
db.help() 查询帮助
db.version() 获取你当前数据库的版本
db.database_name(自己起的集合名字).help() 查询任意数据库的帮助
db.collection_name(自己起的集合名字).find() 查询你当前集合内的信息
创建数据库之后查看没有出现music,
需要写入一个文档集合album;
insert插入 db.album.insertOne({‘title’:’xxxx’}),
查看采用db.album.find();
插入多条数据insertMany([{},{},{}])或者insert([{},{},{}])
2、Collection聚集集合操作
1、创建一个聚集集合
db.createCollection(“collName”, {size: 20, capped: true, max: 100});
db.collName.isCapped(); //判断集合是否为定容量
2、得到指定名称的聚集集合
db.getCollection(“account”);
3、得到当前db的所有聚集集合
db.getCollectionNames();
4、显示当前db所有聚集的状态
db.printCollectionStats();
5、删除集合
db.collectionname(集合名字).drop();
6、添加
db.users.save({name: ‘zhangsan’, age: 25, sex: true});
7、修改(四种方法)
所有数据都添加一个artist
(1)db.albums(集合名).updateMany({},{$set:{artist:‘哈哈’}})
(2)db.users(集合名).update({age: 25}, {KaTeX parse error: Expected 'EOF', got '}' at position 26: …: 'changeName'}}̲, false, true);…inc: {age: 50}}, false, true);
相当于:update users set age = age + 50 where name = ‘Lisi’;
(4)db.users(集合名).update({name: ‘Lisi’}, {$inc: {age: 50}, $set: {name: ‘hoho’}}, false, true);
相当于:update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;
8、删除
db.users(集合名).remove({age: 132});
9、查询
(1)查询所有记录
db.userInfo(集合名).find();
相当于:select* from userInfo;
(2)查询去重后数据
db.userInfo(集合名).distinct(“name”);
相当于:select distict name from userInfo;
(3)查询age = 22的记录
db.userInfo(集合名).find({“age”: 22});
相当于: select * from userInfo where age = 22;
(4)查询age > 22的记录
db.userInfo(集合名).find({age: {KaTeX parse error: Expected 'EOF', got '}' at position 7: gt: 22}̲}); 相当于:select …lt: 22}});
相当于:select * from userInfo where age <22
(6)查询age >= 25的记录
db.userInfo(集合名).find({age: {KaTeX parse error: Expected 'EOF', got '}' at position 8: gte: 25}̲}); 相当于:select …lte: 25}});
(8)查询age >= 23 并且 age <= 26
db.userInfo(集合名).find({age: {$gte: 23, $lte: 26}});
(9)查询name中包含 mongo的数据
db.userInfo(集合名).find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%’;
(10)查询name中以mongo开头的
db.userInfo(集合名).find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
(11)查询指定列name、age数据
db.userInfo(集合名).find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
(12)查询指定列name、age数据, age > 25
db.userInfo(集合名).find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
(13)按照年龄排序
升序:db.userInfo(集合名).find().sort({age: 1});
降序:db.userInfo(集合名).find().sort({age: -1});
(14)查询name = zhangsan, age = 22的数据
db.userInfo(集合名).find({name: ‘zhangsan’, age: 22});
相当于:select * from userInfo where name = ‘zhangsan’ and age = ’22’;
(15)查询前5条数据
db.userInfo(集合名).find().limit(5);
相当于:select top 5 * from userInfo;
(16)查询10条以后的数据
db.userInfo(集合名).find().skip(10);
相当于:select * from userInfo where id not in (
select top 10 * from userInfo
);
(17)限制数据量/几条数据后
db.userInfo(集合名).find().limit(10).skip(5);
(18)or与 查询
db.userInfo(集合名).find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
(19)查询第一条数据
db.userInfo(集合名).findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo(集合名).find().limit(1);
(20)查询某个结果集的记录条数
db.userInfo(集合名).find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
(21)查询某一项的记录数目
db.userInfo(集合名).find({sex: {$exists: true}}).count();
相当于:select count(sex) from userInfo;