MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。
安装前我们需要安装各个 Linux 平台依赖包。
Red Hat/CentOS:
sudo yum install libcurl openssl
MongoDB 源码下载地址:https://www.mongodb.com/download-center#community
这里我们选择 tgz 下载,下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) 。
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.0.tgz # 下载
tar zxvf mongodb-linux-x86_64-rhel70-4.4.0.tgz # 解压
mv mongodb-linux-x86_64-rhel70-4.4.0 /usr/local/mongodb4 # 将解压包拷贝到指定目录
MongoDB 的可执行文件位于 bin 目录下,所以可以将其添加到 PATH 路径中:
export PATH=/bin:$PATH
export PATH=/usr/local/mongodb4/bin:$PATH
默认情况下 MongoDB 启动后会初始化以下两个目录:
我们在启动前可以先创建这两个目录并设置当前用户有读写权限:
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb
sudo chown `whoami` /var/lib/mongo # 设置权限
sudo chown `whoami` /var/log/mongodb # 设置权限
接下来启动 Mongodb 服务:
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
如下应该就是启动成功了。或者打开/var/log/mongodb/mongod.log 文件看到下下图信息,说明启动成功。
如果你需要进入 mongodb 后台管理,你需要先打开 mongodb 装目录的下的 bin 目录,然后执行 mongo 命令文件。
MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。
当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):
[root@VM_50_94_centos /usr/local/mongodb4]# ./bin/mongo
MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9c6ef777-e4f4-480a-951e-8489bd023eee") }
MongoDB server version: 4.4.0
---
The server generated these startup warnings when booting:
2020-08-03T21:35:41.923+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-08-03T21:35:43.001+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-08-03T21:35:43.001+08:00: You are running this process as the root user, which is not recommended
2020-08-03T21:35:43.001+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
(1)由于它是一个javaScript shell,是可以运行一些简单的算术运算的:
> 3*9
27
> 5+10
15
(2)接下来插入一些简单的数据,并对插入的数据进行检索:
> db.runoob.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.runoob.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.runoob.find()
{ "_id" : ObjectId("5f281259e5ebeb4414ea379d"), "x" : 10 }
{ "_id" : ObjectId("5f2816d0047f49b6aa45b78c"), "x" : 10 }
(3)停止mongodb
1)可以使用如下命令:
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --shutdown
2)也可以在shell中实现
> use admin
switched to db admin
> db.shutdownServer()
关于mongodb更多知识可以参见:
https://www.runoob.com/mongodb/mongodb-linux-install.html 系列文章
https://mongoing.com/archives/docs/ mongodb中文社区
(1)insert语句和save语句的区别:
insert只是插入操作;save则可以插入或者修改。当新增数据的主键已经存在时insert会抛主键异常不保存当前数据;而save会对当前数据进行修改操作。
1、首先是启动mongodb
./usr/local/mongodb4/bin/mongo #对于上述安装路径这样既可
#或者也可以这样启动mongodb服务
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
2、显示数据库(两者均可)
show dbs
show databases
3、使用其中的mydb这个数据库
use mydb
4、查看当前使用的数据库
db
5、插入数据(往things集合中插入数据)
db.things.save({_id:1000000,time:1500000001,name:"shuozhuo",hight:181,education:{school:"zhejiang university",educational:"master"}})
db.things.save({_id:1000001,time:1500000002,name:"shuozhuo",hight:180,education:{school:"zhejiang university",educational:"master"}})
db.things.save({_id:1000002,time:1500000003,name:"shuozhuo",hight:180,education:{school:"zhejiang university",educational:"bachelor"}})
db.things.save({_id:1000003,time:1500000004,name:"shuozhuo2",hight:181,education:{school:"zhejiang university",educational:"master"}})
db.things.save({_id:1000004,time:1500000005,name:"shuozhuo3",weight:"60kg",job:{company:"tencent",position:"server",class:6}})
db.things.save({_id:1000005,time:1500000006,name:"shuozhuo3",weight:"70kg",job:{company:"alibaba",position:"server",class:6}})
db.things.save({_id:1000006,time:1500000007,name:"shuozhuo3",weight:"70kg",job:{company:"alibaba",position:"server",class:8}})
db.things.save({_id:1000007,time:1500000008,name:"shuozhuo4",weight:"80kg",job:{company:"baidu",position:"server",class:8}})
db.things.save({_id:1000008,time:1500000009,name:"shuozhuo4",weight:"75kg",job:{company:"baidu",position:"client",class:8}})
注意点:
1)一个集合下_id是唯一的,当_id相同时就会覆盖。
db.things.save({_id:3000000000,name:"shuozhuo"})
6、查看所有数据(查看things集合下的所有数据)
db.things.find();
7、以缩进后的json格式输出——.pretty()!!!!!!
db.things.find({name:"shuozhuo"}).pretty()
注意:只要在语句末尾加上pretty()就都可以
8、删除文档记录
db.things.remove({name:"shuozhuo"})
db.things.remove({}) #删除集合下的所有数据:
9、查询某个字段是否存在
db.things.find({seq:{"$exists":true}})
db.things.find({job:{"$exists":true}})
10、sort排序
db.things.find().sort({time:1}) #升序排列
db.things.find().sort({time:-1}) #降序排列
11、正则表达式匹配
db.things.remove({name:/shuozhuo/})
db.posts.find({name:{$regex:"shuozhuo"}})
12、条件查询/条件筛选:
db.things.find({name:"shuozhuo"}).pretty()
db.things.find({name:"shuozhuo",hight:181}).pretty()
db.things.find({"education.school":"zhejiang university"}).pretty()
#嵌套查询筛选
db.things.find({"education.school":{$eq:"zhejiang university"}}).pretty() #等效上一条
db.things.find({"education.school":"zhejiang university","education.educational":"bachelor"}).pretty()
db.things.find({"education.school":{$eq:"zhejiang university"},"education.educational":{$eq:"bachelor"}}).pretty() #等效上一条
#比较操作(大于、小于、等于、不等于)
db.things.find({time:1500000005}) #等于
db.things.find({"time":{$eq:1500000005}}) #等于
db.things.find({"time":{$gt:1500000005}}) #大于
db.things.find({"time":{$lt:1500000005}}) #小于
db.things.find({"time":{$ne:1500000005}}) #不等于
#逻辑操作(规则很简单就是"$操作符[条件1,条件2,……]"):
#与($and)、或($or),非($not,$nor)
db.things.find({"$and":[{name:"shuozhuo"},{hight:180}]})
db.things.find({"$and":[{"name":{$eq:"shuozhuo"}},{"hight":{$eq:180}}]}) #等效上一行
db.things.find({"$and":[{"name":{$eq:"shuozhuo"}},{"hight":{$eq:180}},{"education.educational":{$eq:"master"}}]})
db.things.find({"$or":[{time:1500000002},{time:1500000003}]})
db.things.find({"$or":[{"time":{$eq:1500000002}},{"time":{$eq:1500000003}}]}) #等效上一行
#更新某个字段:
db.things.update({time:1500000009},{$set:{weight:"100kg"}});
db.things.find({time:1500000009})
db.things.update({time:1500000009},{$set:{"job.class":11}})
db.things.find({time:1500000009})