MongoDB
MongoDB
Section titled “MongoDB”
mongodb,区分于关系型数据库的一大特点,就是自由,不限制于字段的格式,和字段的一致性
-
启动服务建立链接
一、启动数据库
Section titled “一、启动数据库”-
Section titled “mongod 是启动 mongo 的服务”mongod是启动mongo的服务启动服务的时候需要指定,数据库文件保存的路径。
-
Section titled “mongo 是链接 mongo 的客户端”mongo是链接mongo的客户端 -
(1)windows
Section titled “(1)windows”
mongod --dbpath e:/data/dbmongo-
(2)mac
Section titled “(2)mac”
mongod --config /usr/local/etc/mongod.confmongo二、Mongo 的常用命令
Section titled “二、Mongo 的常用命令”1、DB命令
Section titled “1、DB命令”-
**(1)**Help查看命令提示
helpdb.stats()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()
2、集合命令
Section titled “2、集合命令”-
**(1)**创建一个聚集集合
注意:如果超过了 max 设置的最大记录条数,就会将第一条顶掉
db.createCollection("collName",{--最大存储空间为5m,单位kbsize:5242880,--好像4版本之后就没有这个参数了capped:true,--最多存5000条记录max:5000});--获取当前集合db.getCollection("account"); -
(3)得到当前db的所有集合
db.getCollectionNames(); -
**(4)**显示当前db所有集合的状态
db.printCollectionStats(); -
**(5)**删除集合、数据库
db.users.drop();
3、crud方法
Section titled “3、crud方法”添加 save/insert 可批量
Section titled “添加 save/insert 可批量”- insert() 和 save()的区别
-
区别一:
-
insert()直接往库中插入数据,不更新已存在的重复数据只有id相同的情况下,会被认为是重复的数据
-
save()往数据库插入数据时,会更新重复的数据。
-
-
区别二: insert() 可以直接插入一个列表,无需遍历,效率高。 save() 不能直接插入列表,需要遍历列表,逐一插入。
--添加一条db.users.save((name:'zhangsan',age:25,sex:true));--添加多条,使用数组包裹db.users.save([{name:'zhangsan',age:25,sex:true},{name:"kerin",age:100)]);修改 update
Section titled “修改 update”- 参数:
- 参数一(
Object):参数设置查询条件。 - 参数二(
Object):需要通过特定的指令来决定修改类型,之后通过对象写法修改指定字段的值 upsert:为布尔型可选项,表示如果不存在 update 的记录,是否插入这个新的文档。true 为插入;默认为 false,不插入。multi:也是布尔型可选项,默认是 false,只更新找到的第一条记录。如果为 true,则把按条件查询出来的记录全部更新。
- 参数一(
db.users.update( --条件:age等于25的文档 {age:25}, -- 修该name为changeName {$set:{name:'changeName'}}, --默认就是false,不存在的话就不处理 false, --默认false只会修改匹配的第一条数据,主要修改的就是这个, true);--相当于:update users set name='changeName'where age=25;
db.users.update( {name:'Lisi'}, --increment 的缩写可以将指定的number类型的字段在原有的基础上添加 {$inc:{age:50)), false, true);--相当于:update users set age=age+50 where name=Lisi';
db.users.update( {name:'Lisi'}, --将number指定的字段number类型进行减法 {$inc:{age:-50},$set:{name:'kerwin'}}, false, true);--相当于:update users set age=age+50,name=hoho'where name=kerwin';删除 remove
Section titled “删除 remove”db.users.remove({age:132});--删除集合中所有记录db.users.remove()-
参数一: :可选,使用查询操作符指定查询条件
-
参数二: :可选,过滤返回的字段,只显示自己设置的字段
--{name: "zhangsan", address: "天津曲艺协会"}-- 这样的话只会显示 {name: "zhangsan"}-- 0 是不显示,1 是显示db.user.find({}, {name: 1})
条件指令
$gt、$gte、$lt、$lte、$or、count()
(1)查询所有记录db.userlnfo.find();相当于:select * from userlnfo
(2)查询某字段去重后数据db.userlnfo.distinct("name"):相当于:select distict name from userlnfo,
(3)查询age=22的记录db.userlnfo.find({"age":22));相当于:select*from userlnfo where age=22
(4)查询age>22/< 22 的记录db.userlnfo.find({age:{$gt/$lt:22))):相当于:select*from userlnfo where age>22
(5)查询age>=25/age<=25 的记录db.userlnfo.find(fage:{$gte/$Ite:25)));相当于:select*from userlnfo where age>=25
(6)查询age>=23 并且 age<=26db.userlnfo.find(fage:($gte:23,$Ite:26)));
(7)查询当前记录的条数-- total 查询db.userlnfo.find().count()
(8)多条件查询--注意:$or 后面跟的是数组db.userlnfo.find({$or:[{age: 34}, {age: 56}]})-- 要是 and 与条件的话直接在对象内多添加字段即可db.userlnfo.find({name: "zhangsan", age: 34})
(9)单条查询db.userlnfo.findOne()排序sort,
Section titled “排序sort,”-
-1: 是降序
Section titled “-1: 是降序”
db.userinfo.find().sort({age: -1})查询条数 skip,limit
Section titled “查询条数 skip,limit”-
Section titled “skip:跳过多少条”skip:跳过多少条 -
Section titled “limit:查询多少条”limit:查询多少条
--这样的话(currentPage-1) 当第一页的时候就是不跳过,从0开始db.userinfo.sort({age: -1}).skip((currentPage-1) * limit).limit(10)Mongodb 中使用的是正则表达式
- 注意:正则表达式是不需要引号包裹的
(7)查询name中包含mongo的数据,模糊查询db.userlnfo.find({name:/mongo/);--/ 相当于 % 简单说就是正则表达式select from userlnfo where name like '%mongo%';
(8)查询name中以nongo:开头的db.userlnfo.find({name:/^mongo/);select from userlnfo where name like 'mongo%';