MongoDB 入门笔记

用户头像
编程随想曲
关注
发布于: 2020 年 04 月 30 日



​环境

MacOS10.14.4

安装

brew tap mongodb/brewbrew install mongodb-community@4.0

运行

in the foreground

mongod --config /usr/local/etc/mongod.conf

as a macOS service

brew services start mongodb-community@4.0

检查mongodb是否在运行

ps aux | grep -v grep | grep mongod

You can also view the log file to see the current status of your mongod process:

/usr/local/var/log/mongodb/mongo.log

相关路径

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)

连接并使用

mongo

查看版本

./mongo --version

查看所有数据库

show dbs

查看当前数据库

db

切换数据库

use dbname

查看所有集合

show collections

基本操作

1.Create 增

插入一个

db.inventory.insertOne( { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } })

插入多个

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 查

查询单个

db.inventory.find( { item: "canvas" } )



查询所有

db.inventory.find( {} )



利用操作符指定查询条件

db.inventory.find( { status: { $in: [ "A", "D" ] } } )含义等价于SELECT * FROM inventory WHERE status in ("A", "D")



AND

db.inventory.find( { status: "A", qty: { $lt: 30 } } )含义等价于SELECT * FROM inventory WHERE status = "A" AND qty < 30



OR

db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )含义等价于SELECT * FROM inventory WHERE status = "A" OR qty < 30



AND OR

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

db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" }, $currentDate: { lastModified: true } })



update many

db.inventory.updateMany( { "qty": { $lt: 50 } }, { $set: { "size.uom": "in", status: "P" }, $currentDate: { lastModified: true } })



replace one

db.inventory.replaceOne( { item: "paper" }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] })



嵌套查询例子

{a:"",b:"",c:{d:"",e:{f:""}}}}

e作为查询条件查询

find({c.e.f:""})

4.Delete 删

delete all

db.inventory.deleteMany({})



delete many

db.inventory.deleteMany({ status : "A" })



delete one

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/



上一篇

MacOS配置网络命令



欢迎关注我的微信公众号「编程随想曲」



发布于: 2020 年 04 月 30 日 阅读数: 71
用户头像

编程随想曲

关注

学如逆水行舟,不进则退。 2018.03.11 加入

微信公众号「编程随想曲」作者。

评论

发布
暂无评论
MongoDB入门笔记