MongoDB学习笔记(二)常用命令

一、 MongoDB中的术语/概念
这里写图片描述
MongoDB的文档结构为BJSON(Binary Json)格式,如图所示:
这里写图片描述
1. 数据库
一个mongodb中可以建立多个数据库。
MongoDB的单个实例可以容纳多个独立的数据库,每一个都有自己的集合和权限,不同的数据库也放置在不同的文件中。
2. 集合
集合就是 MongoDB 文档组,类似于 RDBMS (关系数据库管理系统:Relational Database Management System)中的表格。
集合存在于数据库中,集合没有固定的结构,这意味着你在对集合可以插入不同格式和类型的数据,但通常情况下我们插入集合的数据都会有一定的关联性。
3. 文档
文档是一个键值(key-value)对。MongoDB 的文档不需要设置相同的字段,并且相同的字段不需要相同的数据类型,这与关系型数据库有很大的区别,也是 MongoDB 非常突出的特点。
一个简单的文档例子如下:
{“genres”: [“犯罪”,”剧情” ],”title”: “肖申克的救赎”}
二、 MongoDB的数据类型
这里写图片描述
三、 数据库的常用命令
1.Help查看命令提示

help
db.help()
db.test.help()
db.test.find().help()

2.创建/切换数据库
use music
3.查询数据库
show dbs
4.查看当前使用的数据库
db/db.getName()
5.显示当前DB状态
db.stats()
6.查看当前DB版本
db.version()
7.查看当前DB的链接机器地址
db.getMongo()
8.删除数据库
db.dropDatabase()
四、 Collection聚集集合操作
1.创建一个聚集集合

db.createCollection("collName", {size: 5242880, capped: true, 
max: 5000}); 最大存储空间为 5m,最多 5000 个文档的集合
db.collName.isCapped(); //判断集合是否为定容量

2.得到指定名称的聚集集合
db.getCollection(“account”);
3.得到当前db的所有聚集集合
db.getCollectionNames();
4.显示当前db所有聚集的状态
db.printCollectionStats();
注意:固定集合有最大存储大小或文档数量,防止超出最大限制。所有固定集合必须指定一个最大存储大小也可以指定一个最大文档数量。如果 collection 达到最大存储大小限制之前达到最大文档数量 MongoDB会删除旧文档。
五、 增删改查操作
1.添加

db.users.save({name: ‘zhangsan', age: 25, sex: true});
db.users.save([{name: ‘zhangsan', age: 25, sex: true},{name:”lisi”,age:100}]);

2.修改

db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
相当于:update users set name = ‘changeName' where age = 25;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
相当于:update users set age = age + 50 where name = ‘Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: lisi}}, false, true);
相当于:update users set age = age + 50, name = ‘hoho' where name = ‘lisi;

db.collection.update( criteria, objNew, upsert, multi )
criteria : update的查询条件,类似sql update查询内where后面的
objNew : update的对象和一些更新的操作符(如 , inc…)等,也可以理解为sql update查询内set后面的
upsert : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

3.删除

db.users.remove({age: 132});

4.查询
(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: {$gt: 22}});
相当于:select * from userInfo where age >22;

(5) 查询age < 22的记录

db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;

(6) 查询age >= 25的记录

db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;

(7) 查询age <= 25的记录

db.userInfo.find({age: {$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数据(想显示哪列,就将字段设置为1)

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) 查询在5-10之间的数据

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;
发布了56 篇原创文章 · 获赞 32 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/hry1243916844/article/details/78124326