1、 使用yum在线安装MongoDB
(1) 配置yum源
root@localhost ~]# vim /etc/yum.repos.d/mongodb.repo
[mongodb]
name=mongodb
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
[root@localhost ~]# yum clean all
(2)安装
[root@localhost ~]# yum install -y mongodb-enterprise
(3)启动
[root@localhost ~]# systemctl start mongod
[root@localhost ~]# systemctl enable mongod
(4)检查
[root@localhost ~]# netstat -lnupt | grep 27017
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1111/mongod
2、源码安装MongoDB并配置服务脚本
(1)上传软件包
[root@localhost ~]# ls
anaconda-ks.cfg mongodb-linux-x86_64-rhel70-4.2.8.tgz
(2)解压
[root@localhost ~]# tar xf mongodb-linux-x86_64-rhel70-4.2.8.tgz -C /usr/local/
(3)创建数据目录
[root@localhost ~]# mkdir -p /data/db
[root@localhost ~]# chown -R mongod:mongod /usr/local/mongodb/*
(4)启动
#创建软链接
[root@localhost ~]# cd /usr/local
[root@localhost local]# ln -sv mongodb-linux-x86_64-rhel70-4.2.8/ mongodb
#添加环境变量
[root@localhost local]# echo "export PATH=$PATH:/usr/local/mongodb/bin" > /etc/profile.d/mongo.sh
[root@localhost local]# source /etc/profile.d/mongo.sh
#命令行启动
[root@localhost ~]# mongod --dbpath /data/db/ &
(5)检查
[root@localhost local]# ps -ef | grep mongod
root 1340 1 1 10:34 ? 00:00:18 /usr/local/mongodb/bin/mongo -f /etc/mongod.conf
root 1429 1270 0 11:00 pts/0 00:00:00 grep --color=auto mongod
(6)以系统服务方式启动
- 创建配置文件(将RPM安装的配置文件拷贝过来修改)
[root@localhost ~]# vim /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/db/
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
- 配置服务启动脚本(将RPM安装的配置文件拷贝过来修改)
[root@localhost ~]# vim /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/local/mongodb/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
- 创建mongod的组和用户(参照RPM安装的用户和组创建)
[root@localhost ~]# groupadd -g 995 -r mongod
[root@localhost ~]# useradd -r -u 995 -g 995 -d /var/lib/mongo -c 'mongod' -s /bin/false mongod
- 按照配置文件及服务脚本创建目录并给予权限
#日志文件
[root@localhost ~]# mkdir /var/log/mongodb -p
[root@localhost ~]# touch mongod.log
[root@localhost ~]# chown -R mongod:mongod var/log/mongodb/
#数据文件
[root@localhost ~]# chown -R mongod:mongod /data/db/
#pid文件
[root@localhost ~]# chown -R mongod:mongod /var/run/mongodb/
- 测试服务脚本
[root@localhost ~]# kill `pidof mongod`
[root@localhost ~]# systemctl start mongodb
[root@localhost ~]# systemctl enable mongodb
[root@localhost ~]# lsof -i tcp:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 1565 root 11u IPv4 22460 0t0 TCP localhost:27017 (LISTEN)
3、基本操作作业
1)创建一个数据库 名字grade
> use grade
switched to db grede
2)数据库中创建一个集合名字 class
> db.createCollection("class")
{ "ok" : 1 }
3)集合中插入若干数据 文档格式如下
{name:'zhang',age;10,sex:'m',hobby:['a','b','c']} hobby: draw sing dance basketball football pingpong computer
自行插入数据,后面查询使用
> db.class.insertMany([
... {name:'小明',age:10,sex:'m',hobby:['draw','computer','football']},
... {name:'莉莉',age:6,sex:'f',hobby:['sing','pingpong']},
... {name:'李华',age:12,sex:'m',hobby:['dance','computer','basketball']},
... {name:'小龙',age:8,sex:'m',hobby:['pingpong','sing']},
... {name:'婷婷',age:11,sex:'f',hobby:['computer','basketball']},
... {name:'小红',age:7,sex:'f',hobby:['sing','football','computer']},
... {name:'小李',age:9,sex:'m',hobby:['sing','pingpong','draw']},
... {name:'小王',age:8,sex:'f',hobby:['dance','sing','basketball']},
... {name:'小红',age:12,sex:'m',hobby:['pingpong','sing']}
... ])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5f2501bf0befe8bd33566126"),
ObjectId("5f2501bf0befe8bd33566127"),
ObjectId("5f2501bf0befe8bd33566128"),
ObjectId("5f2501bf0befe8bd33566129"),
ObjectId("5f2501bf0befe8bd3356612a"),
ObjectId("5f2501bf0befe8bd3356612b"),
ObjectId("5f2501bf0befe8bd3356612c"),
ObjectId("5f2501bf0befe8bd3356612d"),
ObjectId("5f2501bf0befe8bd3356612e")
]
}
4)查询操作
查看班级所有人信息
> db.class.find()
{ "_id" : ObjectId("5f2501bf0befe8bd33566126"), "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "draw", "computer", "football" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566127"), "name" : "莉莉", "age" : 6, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566128"), "name" : "李华", "age" : 12, "sex" : "m", "hobby" : [ "dance", "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566129"), "name" : "小龙", "age" : 8, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "sex" : "f", "hobby" : [ "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 7, "sex" : "f", "hobby" : [ "sing", "football", "computer" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 9, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612e"), "name" : "小红", "age" : 12, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
查看班级中年龄为8岁的学生信息
> db.class.find({age:8})
{ "_id" : ObjectId("5f2501bf0befe8bd33566129"), "name" : "小龙", "age" : 8, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball" ] }
查看年龄大于10岁的学生信息
> db.class.find({age:{$gt:10}})
{ "_id" : ObjectId("5f2501bf0befe8bd33566128"), "name" : "李华", "age" : 12, "sex" : "m", "hobby" : [ "dance", "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "sex" : "f", "hobby" : [ "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612e"), "name" : "小红", "age" : 12, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
查看年龄在 4~8岁之间的学生信息
> db.class.find({age:{$gte:4,$lte:8}})
{ "_id" : ObjectId("5f2501bf0befe8bd33566127"), "name" : "莉莉", "age" : 6, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566129"), "name" : "小龙", "age" : 8, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 7, "sex" : "f", "hobby" : [ "sing", "football", "computer" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball" ] }
找到年龄为6岁且为男生的学生
> db.class.find({age:6,sex:'m'})
#无结果
找到年龄小于7岁或者大于10岁的学生
> db.class.find({$or:[{age:{$lt:7}},{age:{$gt:10}}]})
{ "_id" : ObjectId("5f2501bf0befe8bd33566127"), "name" : "莉莉", "age" : 6, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566128"), "name" : "李华", "age" : 12, "sex" : "m", "hobby" : [ "dance", "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "sex" : "f", "hobby" : [ "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612e"), "name" : "小红", "age" : 12, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
找到年龄是8岁或者11岁的学生
> db.class.find({age:{$in:[8,11]}})
{ "_id" : ObjectId("5f2501bf0befe8bd33566129"), "name" : "小龙", "age" : 8, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "sex" : "f", "hobby" : [ "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball" ] }
找到兴趣爱好有两项的学生
> db.class.find({hobby:{$size:2}})
{ "_id" : ObjectId("5f2501bf0befe8bd33566127"), "name" : "莉莉", "age" : 6, "sex" : "f", "hobby" : [ "sing", "pingpong" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd33566129"), "name" : "小龙", "age" : 8, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "sex" : "f", "hobby" : [ "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612e"), "name" : "小红", "age" : 12, "sex" : "m", "hobby" : [ "pingpong", "sing" ] }
找到兴趣爱好有draw的学生
> db.class.find({hobby:'draw'})
{ "_id" : ObjectId("5f2501bf0befe8bd33566126"), "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "draw", "computer", "football" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 9, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw" ] }
找到既喜欢画画又喜欢跳舞的学生
> db.class.find({hobby:{$all:['draw','dance']}})
#无结果
统计爱好有三项的学生人数
> db.class.find({hobby:{$size:3}}).count()
5
找出本班年龄第二大的学生
> db.class.find().sort({age:1}).skip(7).limit(1)
{ "_id" : ObjectId("5f2501bf0befe8bd33566128"), "name" : "李华", "age" : 12, "sex" : "m", "hobby" : [ "dance", "computer", "basketball" ] }
查看学生的兴趣范围
> db.class.distinct("hobby")
[
"basketball",
"computer",
"dance",
"draw",
"football",
"pingpong",
"running",
"sing",
[
"吹牛",
"basketball"
]
]
将学生按年龄排序找到年龄最大的三个
> db.class.find().sort({age:-1}).limit(3)
{ "_id" : ObjectId("5f2501bf0befe8bd33566128"), "name" : "李华", "age" : 12, "s ex" : "m", "hobby" : [ "dance", "computer", "basketball" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612e"), "name" : "小红", "age" : 12, "s ex" : "m", "hobby" : [ "pingpong", "sing" ] }
{ "_id" : ObjectId("5f2501bf0befe8bd3356612a"), "name" : "婷婷", "age" : 11, "s ex" : "f", "hobby" : [ "computer", "basketball" ] }
删除所有年龄大于12或者小于4岁的学生
> db.class.remove({$or:[{age:{$gt:12}},{age:{$lt:4}}]})
WriteResult({ "nRemoved" : 0 })
5)增加、更新、删除、统计
将小红的年龄变为8岁 兴趣爱好变为 跳舞 画画
> db.class.find({name:'小红'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 7, "seall", "computer" ] }
> db.class.update( {name:"小红"}, {$set:{age:8} })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.update( {name:"小红"}, {$set:{hobby:['draw','dance']}} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小红'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 8, "sex" : "f", "hobby" : [ "draw", "dance" ] }
追加小明兴趣爱好:唱歌
> db.class.update({name:'小明'},{$push:{hobby:'sing'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小明'})
{ "_id" : ObjectId("5f2501bf0befe8bd33566126"), "name" : "小明", "age" : 10, "sex" : "m", "hobby" : [ "draw", "computer", "football", "sing" ] }
小王兴趣爱好增加吹牛,篮球
> db.class.find({name:'小王'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball" ] }
> db.class.update({name:'小王'},{$push:{hobby:['吹牛','basketball']}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小王'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612d"), "name" : "小王", "age" : 8, "sex" : "f", "hobby" : [ "dance", "sing", "basketball", [ "吹牛", "basketball" ] ] }
小李增加爱好,跑步和唱歌,但是不要和以前的重复
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 9, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw" ] }
> db.class.update({name:'小李'},{$addToSet:{hobby:{$each:['running','sing']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 9, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw", "running" ] }
该班所有同学年龄加1
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 9, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw", "running" ] }
> db.class.update({},{$inc:{age:1}},false,true)
WriteResult({ "nMatched" : 7, "nUpserted" : 0, "nModified" : 7 })
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 10, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw", "running" ] }
删除小明的sex属性
> db.class.update({name:'小明'},{$unset:{sex:0}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小明'})
{ "_id" : ObjectId("5f2501bf0befe8bd33566126"), "name" : "小明", "age" : 11, "hobby" : [ "draw", "computer", "football", "sing", [ "吹牛", "basketball" ] ] }
删除小李兴趣中的第一项
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 10, "sex" : "m", "hobby" : [ "sing", "pingpong", "draw", "running" ] }
> db.class.update({name:'小李'},{$pop:{hobby:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小李'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612c"), "name" : "小李", "age" : 10, "sex" : "m", "hobby" : [ "pingpong", "draw", "running" ] }
将小红兴趣中的画画爱好删除
> db.class.find({name:'小红'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "draw", "dance" ] }
> db.class.update({name:'小红'},{$pull:{hobby:'draw'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.class.find({name:'小红'})
{ "_id" : ObjectId("5f2501bf0befe8bd3356612b"), "name" : "小红", "age" : 9, "sex" : "f", "hobby" : [ "dance" ] }