目录
1、下载安装
官网下载安装介质:https://www.mongodb.com/download-center,选择适当的版本,这里以linux版本为例
解压到系统某路径, tar -xvzf mongodb-linux-x86_64-rhel70-3.4.10.tgz
并在安装目录创建data目录,以及logs目录和logs/mongodb.log文件
使用vim在mongodb的bin目录创建mongodb的配置文件,如:vim bin/mongodb.conf
storage:
dbPath: "/usr/local/apache/mongoDB/mongodb-linux-x86_64-rhel70-3.4.10/data"
systemLog:
destination: file
path: "/usr/local/apache/mongoDB/mongodb-linux-x86_64-rhel70-3.4.10/logs/mongodb.log"
net:
port: 27022
http:
RESTInterfaceEnabled: true
processManagement:
fork: false
新建完后:
赋权:
chmod 777 mongodb.conf
编写shell脚本,命名为start-mongodb.sh
nohup ./mongod -f mongodb.conf &
chmod 777 start-mongodb.sh
关闭防火墙:
systemctl stop firewalld.service
还得到阿里云安全组去设置,允许mongoDb的端口访问
2、启动运行
启动服务 :
./start-mongodb.sh
通过浏览器访问,注意端口加1000,mongodb.conf中的端口是27022,但浏览器端口是28022:
http://IP地址:28022/
证明安装成功了
连接命令:
./mongo localhost:27022
如果没有成功启动,可以看日志:
vi nohup.out
停止命令:
./mongod --shutdown --dbpath /usr/local/apache/mongoDB/mongodb-linux-x86_64-rhel70-3.4.10/data
样本数据练习数据:
db.users.drop();
var user1 = {
"username" : "lison",
"country" : "china",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["杀破狼2","战狼","雷神1"],
"cites" : ["长沙","深圳","上海"]
},
"age" : 18,
"salary":NumberDecimal("18889.09"),
"lenght" :1.79
};
var user2 = {
"username" : "sean",
"country" : "English",
"address" : {
"aCode" : "311000",
"add" : "地址"
},
"favorites" : {
"movies" : ["复仇者联盟","战狼","雷神1"],
"cites" : ["西安","东京","上海"]
},
"age" : 24,
"salary":NumberDecimal("7889.09"),
"lenght" :1.35
};
var user3 ={
"username" : "jack",
"country" : "japan",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["肉蒲团","一路向西","倩女幽魂"],
"cites" : ["东莞","深圳","东京"]
},
"age" : 22,
"salary":NumberDecimal("6666.66"),
"lenght" :1.85
};
var user4 =
{
"username" : "mark",
"country" : "USA",
"address" : {
"aCode" : "411000",
"add" : "长沙"
},
"favorites" : {
"movies" : ["蜘蛛侠","钢铁侠","蝙蝠侠"],
"cites" : ["青岛","东莞","上海"]
},
"age" : 20,
"salary":NumberDecimal("6398.22"),
"lenght" :1.77
};
var user5 =
{
"username" : "test",
"country" : "UK",
"address" : {
"aCode" : "411000",
"add" : "TEST"
},
"favorites" : {
"movies" : ["蜘蛛侠","钢铁侠","蝙蝠侠"],
"cites" : ["青岛","东莞","上海"]
},
"salary":NumberDecimal("1969.88")
};
db.users.insert(user1);
db.users.insert(user2);
db.users.insert(user3);
db.users.insert(user4);
db.users.insert(user5);
练习命令:
in语句示例:
db.users.find({"username":{"$in":["lison","sean"]}})
not语句示例:
db.users.find({"lenght":{"$not":{"$gte":1.77}}}).pretty()
因为not语句 会把不包含查询语句字段的文档 也检索出来
exists语句示例:
db.users.find({"lenght":{"$exists":true}}).pretty()
判断文档有没有关心的字段
使用,robomongo进行连接,下载地址::
https://robomongo.org/download