macOS下安装mongoDB
- 从官网上下载压缩包 www.mongodb.com/download-ce…
选择macOS x64
- 解压mongodb-macos-x86_64-6.0.3.tgz后
- 将mongodb-macos-x86_64-6.0.3文件夹重命名为mongodb
- 将mongodb文件放入/usr/local/中。
- 打开finder,快捷键command+shift+g
- 输入/usr/local/后回车
- 粘贴将mongodb文件夹
- 创建存储data的文件夹和存储log的文件夹
- 打开finder,快捷键command+shift+g
- 配置环境变量
-
如果是.bash 命令行输入下列代码:
open .bash_profile新增环境变量配置,并command+s保存。export PATH=${PATH}:/usr/local/mongodb/bin然后在命令行中输入如下代码使配置生效:source .bash_profile -
如果是zsh 在命令行中输入如下代码打开.zshrc文件
open .zshrc
在文件中新增如下配置,并command+s保存。
export PATH=/usr/local/mongodb/bin:$PATH
source .bash_profile
然后在命令行中输入如下代码使配置生效:
source .zshrc
启动mongodb服务
mongod --fork --dbpath /usr/local/mongodb/data --logpath /usr/local/mongodb/log/mongo.log --logappend
- --dbpath /usr/local/mongodb/data 是设置数据存储的路径
- --logpath /usr/local/mongodb/log/mongo.log --logappend 是设置log日志存储的路径,并且log为累加规则
运行完以上规则后,可以看到如下消息:
并且访问mongodb默认端口(http://localhost:27017/)会得到如下结果
说明mongdb服务已启动(但是不能用http协议访问)
快捷启动
在/usr/local/mongodb 下创建一个etc文件夹,并创建mongodb.conf文件。
config配置文件
# 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: /usr/local/mongodb/log/mongo.log
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb/data/
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
security:
authorization: enabled
启动命令
mongod -f /usr/local/mongodb/etc/mongodb.conf
配置文件设置用户
当我们使用快捷启动时,配置文件中有这样一行代码:
security:
authorization: enabled
表示需要校验用户权限。 所以需要给数据库设置用户,并登陆
db.createUser({"user":"root","pwd":"123456","roles":[{role:"root",db:"admin"}]
db.auth('root','123456')
停止mongodb服务
请勿暴力关闭命令行,一定要通过命令行停止mongodb服务,否则可能会起不起来。
注意:停止服务需要用mongo命令,但是因为6.0.x版本的mongodban安装包中已经没有这个终端命令,所以
需要从5.0.x安装包中获取:
按照上面的安装流程再次安装5.0.x版本,并把该文件夹中bin/mongo文件粘贴到我们配置好的 /usr/local/mongodb/bin中。
另外起一个终端,输入如下命令即可终止服务:
mongo
db
use admin
db.shutdownServer()
安装可视化工具
mongodb-compass 下载地址:www.mongodb.com/try/downloa…
安装好后配置connect url 即可链接上
操作mongodb数据库
基本概念
数据库(database):不需要手动创建
集合(collection):不需要手动创建
文档(document):创建文档时,如果文档所在的集合或者数据库不存在会自动创建数据库和集合
官方API文档: www.mongodb.com/docs/manual…
指令
基础指令
-
show dbs: 显示当前服务器下的数据库
-
show databases: 显示当前服务器下的数据库
-
use 数据库名:进入数据库
-
db: 输出当前数据库
-
show collections: 显示数据库中所有集合
CRUD 增删改查
增加
插入文档
// 单个插入
db.<collectionName>.insert({
name: "hello mongoDB"
})
// 多个插入
db.<collectionName>.insert([{
name: "hello mongoDB"
},
{
name: "hello man"
}
])
mongoDB会自动生成_id: ObjectId("637dd00d114e374d6dbf8dc3"),作为每个文档的唯一标识。
同时我也可以自定义_id, 但是必须确保唯一性。
db.<collectionName>.insert({
_id: "1234567",
name: "hello mongoDB"
})
其他方法:
db.<collectionName>.insertOne():插入一个文档对象
db.<collectionName>.insertMany():插入多个文档对象
查询
- 命令行查询
db.<collectionName>.find()
- 可视化工具查询
带条件的查询
db.<collectionName>.find({
name: "hello man!"
})
只查一条
db.<collectionName>.findOne({
name: "hello man!"
})
查询文档数量
db.<collectionName>.find().count()
or
db.<collectionName>.find().length()
修改文档
db.<collectionName>.update(查询条件,新对象)
db.<collectionName>.update({
name: "hello man!"
}, {
name: "hello young man!"
})
但是这个方法默认情况下是覆盖写,而不是追加写!!!
-
修改操作符
$set: 修改制定属性
db.<collectionName>.update({
name: "hello man!"
}, {
$set: {
age: 18
}
})
$unset: 删除属性
updateMany: 因为update默认只会改一个文档,如果需要改查出的全部,就需要使用updateMany。
db.<collectionName>.updateMany({
name: "hello man!"
}, {
$set: {
age: 18
}
})
删除文档
删除所有文档db.inventory.deleteMany({})
删除部分文档:
db.inventory.deleteMany({
name: "hello man!"
})
db.inventory.deleteOne({
name: "hello man!"
})