环境
MacOS10.14.4
安装
代码语言:javascript复制brew tap mongodb/brew
brew install mongodb-community@4.0
运行
in the foreground
代码语言:javascript复制mongod --config /usr/local/etc/mongod.conf
as a macOS service
代码语言:javascript复制brew services start mongodb-community@4.0
检查mongodb是否在运行
代码语言:javascript复制ps aux | grep -v grep | grep mongod
You can also view the log file to see the current status of your mongod process:
代码语言:javascript复制 /usr/local/var/log/mongodb/mongo.log
相关路径
代码语言:javascript复制the configuration file (/usr/local/etc/mongod.conf)
the log directory path (/usr/local/var/log/mongodb)
the data directory path (/usr/local/var/mongodb)
连接并使用
代码语言:javascript复制mongo
查看版本
代码语言:javascript复制./mongo --version
查看所有数据库
代码语言:javascript复制show dbs
查看当前数据库
代码语言:javascript复制db
切换数据库
代码语言:javascript复制use dbname
查看所有集合
代码语言:javascript复制show collections
基本操作
1.Create 增
插入一个
代码语言:javascript复制db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
插入多个
代码语言:javascript复制db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
2.Read 查
查询单个
代码语言:javascript复制db.inventory.find( { item: "canvas" } )
查询所有
代码语言:javascript复制db.inventory.find( {} )
利用操作符指定查询条件
代码语言:javascript复制db.inventory.find( { status: { $in: [ "A", "D" ] } } )
含义等价于
SELECT * FROM inventory WHERE status in ("A", "D")
AND
代码语言:javascript复制db.inventory.find( { status: "A", qty: { $lt: 30 } } )
含义等价于
SELECT * FROM inventory WHERE status = "A" AND qty < 30
OR
代码语言:javascript复制db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
含义等价于
SELECT * FROM inventory WHERE status = "A" OR qty < 30
AND OR
代码语言:javascript复制db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
含义等价于
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
3.Update 改
update one
代码语言:javascript复制db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
update many
代码语言:javascript复制db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
replace one
代码语言:javascript复制db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
嵌套查询例子
代码语言:javascript复制{a:"",b:"",c:{d:"",e:{f:""}}}}
e作为查询条件查询
代码语言:javascript复制find({c.e.f:""})
4.Delete 删
delete all
代码语言:javascript复制db.inventory.deleteMany({})
delete many
代码语言:javascript复制db.inventory.deleteMany({ status : "A" })
delete one
代码语言:javascript复制db.inventory.deleteOne( { status: "D" } )
References
[1]
Install MongoDB Community Edition on macOS: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
[2]
insert-documents: https://docs.mongodb.com/manual/tutorial/insert-documents/
[3]
query-documents: https://docs.mongodb.com/manual/tutorial/query-documents/
[4]
update-documents: https://docs.mongodb.com/manual/tutorial/update-documents/
[5]
remove-documents: https://docs.mongodb.com/manual/tutorial/remove-documents/
[6]
MongoDB数据库命令行操作: https://blog.csdn.net/qq_33036599/article/details/83061446
[7]
题图: https://unsplash.com/