基本概念
数据库(database)
集合(collecetion)
文档(document)
在MongoDb中无需创建数据库,会在第一次插入文档时出现
基本指令
#显示所有数据库
show dbs
show databases
#使用数据库
use 数据库名
db #显示当前所使用的数据库
show collections #显示数据库的所有集合
#数据库的CRUD操作(https://docs.mongodb.com/manual/crud/)
#插入数据
db.<collection>.insertOne()
eg: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" } }
])
#查询数据
db.<collection>.find() #查询指定集合的所有文档
eg:db.inventory.find()
db.<collection>.find({item:"canvas"}) #查询指定集合的所有文档
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
--> SELECT * FROM inventory WHERE status in ("A", "D")
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
-->SELECT * FROM inventory WHERE status = "A" AND qty < 30
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
-->SELECT * FROM inventory WHERE status = "A" OR qty < 30
db.inventory.find( {status: "A", $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]} )
-->SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")