MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 json 的 bson 格式,因此可以存储比较复杂的数据类型。Mongo 最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。它的特点是高性能、易部署、易使用,存储数据非常方便。
1、数据库创建和服务的开启
(1)、安装完成后,需要将安装路径下的bin目录添加到环境变量path中,然后打开命令行窗口,输入mongo,看到版本信息等表示成功。
(2)、启动mongodb:新建一个存储mongodb数据的目录,路径不能有中文和空格,然后在命令行窗口,输入“mongod --dbpath 数据的目录路径”即可。
(3)、打开另外一个命令行窗口,输入“mongo”,即可连入本地mongo数据库。
(4)、连接远程数据库:mongo 127.0.0.1:27017(ip加端口)
2、数据库和表的创建,及常用命令
(1)、显示当前有哪些数据库:show dbs
(2)、使用数据库,即使数据库不存在,也可以use:use student
(3)、通过insert的方式,创建表,并且插入数据:db.user.insert({"name": "zhangsan", "age": 20})
(4)、查询当前数据库所有集合,所有表:show collections
(5)、删除数据库:db.dropDatabase()
(6)、删除集合,删除指定的集合,删除表:db.user.drop()
3、查询
1、查询所有记录:db.userInfo.find();
相当于:select* from userInfo;
2、查询去掉后的当前聚集集合中的某列的重复:db.userInfo.distinct("name");
会过滤掉 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 数据:db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
当然 name 也可以用 true 或 false,当用 ture 的情况下河 name:1 效果一样,如果用 false 就是排除 name,显示 name 以外的列信息。
12、查询指定列 name、age 数据, age > 25:db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
13、按照年龄排序 1 升序 -1 降序
升序: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);
可用于分页,limit 是 pageSize,skip 是第几页*pageSize
18、or 与 查询:db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
19、findOne 查询第一条数据: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;
如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0) :db.users.find().skip(10).limit(5).count(true)
4、更新
1、只更新一条:db.userInfo.update({name: "张三"}, {$set: {age: 20}})
2、更新全部:db.userInfo.update({name: "张三"}, {$set: {age: 20}}, {multi: true})
5、删除
(1)、删除数据库:先use 数据库,然后在:db.dropDatabase()
(2)、删除集合,删除指定的集合,删除表:db.userInfo.drop()
(3)、删除数据:db.userInfo.remove({name: "张三"}),
(4)、只删除一条:db.userInfo.remove({name: "张三"}, {justOne: true})
导出导入:
(1)导出:mongodump -h 数据库地址-d 数据库名称 -o 导出数据库文件的路径
栗子:mongodump -h 127.0.0.1 -d mongotest -o C:/test
(2)导入:mongorestore -h 数据库地址-d 数据库名称(起一个数据库名称,会自动创建) 导入数据库文件的路径
栗子:mongorestore -h 127.0.0.1 -d mongotest C:/test