Linux安装命令
touch /etc/yum.repos.d/mongodb-org-3.4.repo
将下面内容添加到创建的文件中
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
yum命令从国外服务器下载并安装
sudo yum install -y mongodb-org
为防止自动更新,在/etc/yum.conf中加上如下行:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
找到编辑
/etc/mongod.conf
将bind_ip=127.0.0.1 改成0.0.0.0
1.启动mongo
sudo service mongod start
service iptables stop
如果启动过了使用
sudo service mongod restart
service iptables stop
配置开机启动
chkconfig mongod on
service iptables stop
mongo localhost:27017
mongo部署
1)配置环境变量
2)在D盘建一个数据文件夹data
3) 命令启动服务器27017端口
mongod --dbpath=d:/data
不要关闭dos窗口
4) 客户端连接进入mongodb命令窗口
mongo localhost:27017
默认连接进去先进的是test库
db; 显示当前在哪个库里面
use shop; 创建一个库名叫shop的数据库
//隐式创建表集
db.userInfo.insert({"name":"zhangsan","age":18}); //添加数据
db.userInfo.find(); //find里面没有加条件是查找该表集的全部数据
db.createCollection("product"); //显示创建名字叫product的表集
show collections; //查看当前库下面有哪些表集
下面添加10000行数据
for(var i=1;i<=10000;i++){
db.product.insert({"pName":"name"+i,"price":i})
}
//统计一下该表中有多少个文档数据
db.product.count();
//默认返回随机的20条文档数据
db.product.find();
//第一个json大括号里面加的是条件过滤
db.product.find({"price":10});
//第二个json大括号里面是不希望哪些列查询出来就设置成0
db.product.find({"price":10},{"_id":0});
//查询前十条文档数据
db.product.find().skip(0).limit(10);
//不希望显示哪些列 设成0
db.product.find({},{"_id":0}).skip(0).limit(10);
//先使用find过滤和排除哪些列再使用sort进行排序
db.product.find({},{"_id":0}).sort({"price":1}).skip(0).limit(10);
//更新要注意
db.product.update({"price":100},{"price":99})
//为避免有些列被冲掉要先查找出来修改局部之后再进行更新
db.product.update({"price":101},{"pName":"name101","price":99})
//如果没加条件是全部清空移除
db.product.remove()
//移除满足条件的元素
db.product.remove({"price":104})
db.product.update({"price":105},{"pName":"name105","price":106})
db.product.remove({"price":106}) 满足条件的全部数据文档行都会被移除
使用即创建
use shop 创建库shop
db.goods.insert({name:"apple",price:2.6}) 创建product表并同时往里面填数据
db.product.find({第一个json是过滤条件},{第二个json是显示哪些列})
第二个参数特地写:0不显示
//备份 必须在mongoDB命令外部,先退出到cmd命令窗口
exit;
mongodump -h localhost:27017 -d shop -o D://bak
linux下使用
mongodump -h localhost:27017 -d shop -o /usr/bak
db 默认在test库下
use shop
db.dropDatabase();
show dbs;
恢复
exit;
mongorestore -h localhost:27017 -d shop -directoryperdb D://bak/shop
//添加个大数据500万个文档对象数据行
for(var i=1;i<=5000000;i++){
db.userInfo.insert({"userName":"name"+i,"userNO":"NO"+i})
}
//加索引之前查比较慢
db.userInfo.find({"userNO":"NO100"})
//查询耗时
db.userInfo.find({"userNO":"NO100"}).explain()
//添加索引 给userNO列添加索引
db.userInfo.ensureIndex({"userNO":1});
//添加索引之后再查加倍查询速度
db.userInfo.find({"userNO":"NO100"})
//查询耗时几乎为0
db.userInfo.find({"userNO":"NO100"}).explain()
//查看索引
db.system.indexes.find()
//大于: field > value
db.product.find({"price":{$gt:9996}});
//大于等于: field >= value
db.product.find({"price":{$gte:9996}});
//小于: field < value
db.product.find({"price":{$lt:10}});
//小于等于: field <= value
db.product.find({"price":{$lte:10}});
//不等于: field != value
db.product.find({"price":{$ne:10}});
db.product.find({$and:[{"price":{$ne:4}},{"price":{$ne:13}}]}).skip(0).limit(20)
db.product.count();
db.product.find().skip(索引).limit(pageSize);
包含索引所在的那一条
分页 前十条
db.product.find()..skip(0).limit(10);
分页取接下来的十条
db.userInfo.find().skip(10).limit(10);
db.product.find({},{"_id":0}).sort({"price":1}).skip(0).limit(10)
db.product.find({price:{$ne:4}},{"_id":0}).skip(0).limit(10);
db.userInfo.find({age:{$lt:5}}).count();
db.userInfo.find().sort({age:1}); 降序-1
db.userInfo.find({},{"userName":1,"age":1,"_id":0}).sort({age:-1}).skip(0).limit(10);
db.userInfo.find().skip(2).limit(3);
db.userInfo.find().sort({age:-1}).skip(2).limit(3);
db.userInfo.find().sort({age:-1}).skip(2).limit(3).count();
查询集合中的文档 ,$or,相当于关系型数据库中的OR,表示或者的关系,
例如查询name为user2或者age为3的文档,命令为:
db.userInfo.find({$or:[{userName:"test2"},{age:3}]})
db.userInfo.find({$and:[{age:{$lt:17}},{age:{$gt:10}}]})
http://www.mongodb.org/downloads