2

MongoDB 基本用法

 2 years ago
source link: https://ppsteven.github.io/pages/6fdfeb/#%E8%A1%A5%E5%85%85
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MongoDB 基本用法

记得最早使用的数据库就是MongoDB,当时懵懂无知,就知道需要用一个数据库去储存数据,也分不清SQL 和 NoSQL。不过MongoDB以它的方便快捷吸引了我,以至于后来每次用MySQL的时候,还奇怪为啥每次都需要DDL定义结构,好麻烦。

P.S. 还记得学完MongoDB的那个晚上,黄昏🌆还是蛮好看的样子

# 安装教程

# 基础命令

参考教程推荐:CRUD Guides: Create, Read, Update, and Delete Data

# 数据库&集合

use test # 切换数据库
show dbs # 显示所有数据库
show collections # 显示所有collections

db.dropDatabase(test) # 删除数据库 test
db.createCollection(inventory) # 创建集合 inventory
db.inventory.drop() # 删除集合 inventory


mongo mongodb://$[hostlist]/$[database]?authSource=$[authSource] --username $[username] # 连接数据库
1
2
3
4
5
6
7
8
9
10

# 查询文档

# 查询全量数据
db.inventory.find({}) 

# 带Query的查询
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } ) 
db.inventory.find( { "size.uom": "in" } ) # 使用 dot notation

# 带比较运算符的查询
db.inventory.find( { "size.h": { $lt: 15 } } )

# 带And的查询
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )  # 显式写法
db.inventory.find( { price: { $ne: 1.99, $exists: true } } )  # 隐式写法

# 带Or的查询
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )


# 复杂查询
# AND 和 OR 一起使用
db.inventory.find( {
    $and: [
        { $or: [ { qty: { $lt : 10 } }, { qty : { $gt: 50 } } ] },
        { $or: [ { sale: true }, { price : { $lt : 5 } } ] }
    ]
} )


db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]  // qty小于30 或 item 以 p 开头
} 

# 输出格式化
db.inventory.find({}).pretty()


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 插入文档

# use test
# 将数据插入collections:inventory,此时会创建test数据库
# 插入一条数据
db.inventory.insertOne(
   { "item" : "canvas",
     "qty" : 100,
     "tags" : ["cotton"],
     "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }
   }
)

# 批量插入数据
db.inventory.insertMany( [
   { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
   { "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
   { "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
   { "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
   { "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 更新文档

# 更新
# 更新一条
db.inventory.updateOne(
    { "item" : "paper" }, // specifies the document to update
    {
      $set: {  "size.uom" : "cm",  "status" : "P" },
      $currentDate: { "lastModified": true }
    }
)

# 更新多条
db.inventory.updateMany(
    { "qty" : { $lt: 50 } }, // specifies the documents to update
    {
       $set: { "size.uom" : "cm", "status": "P" },
       $currentDate : { "lastModified": true }
    }
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 删除文档

# 删除一条
db.inventory.deleteOne(
    { "status": "D" } // specifies the document to delete
)
# 删除多条
db.inventory.deleteMany(
    { "status" : "A" } // specifies the documents to delete
)
1
2
3
4
5
6
7
8

# 补充

  • MongoDB中存在许多运算符,如前 $gtQuery and Projection Operators

  • 通过编程语言连接 MongoDB 文档:https://docs.mongodb.com/drivers/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK