【云圖說】第235期 DDS讀寫兩步走 帶您領(lǐng)略只讀節(jié)點(diǎn)的風(fēng)采
693
2025-04-01
MongoDB 基本操作
當(dāng)前文檔基于 MongoDB server version: 4.0.20
0. MongoDB 特性
用MySQL 和 MongoDB 做比較
1. 數(shù)據(jù)庫操作
在mongoDB中, 若數(shù)據(jù)庫不存在,那么只有在庫中向集合插入數(shù)據(jù)的時(shí)候才會(huì)創(chuàng)建
1.1 用法
查看數(shù)據(jù)庫
show dbs
指定數(shù)據(jù)庫
use Database
查看當(dāng)前數(shù)據(jù)庫
默認(rèn)情況下,當(dāng)前數(shù)據(jù)庫是test
db
刪除數(shù)據(jù)庫
在操作此步驟之前,應(yīng)當(dāng)使用 db 確認(rèn)一下庫名
執(zhí)行dropDatabase() 操作后,當(dāng)前選擇的數(shù)據(jù)庫依然是這個(gè)庫,只不過是在內(nèi)存中而已。
db.dropDatabase()
查看數(shù)據(jù)庫版本
db.version()
查看當(dāng)前庫統(tǒng)計(jì)信息
db.stats()
1.2 例子
查看當(dāng)前有哪些數(shù)據(jù)庫
> show dbs admin 0.000GB blogSystem 0.000GB config 0.000GB local 0.000GB >
查看當(dāng)前數(shù)據(jù)庫
> db test >
創(chuàng)建一個(gè)數(shù)據(jù)庫并且查看
> use LearnDB switched to db LearnDB > > db LearnDB >
刪除數(shù)據(jù)庫
> db.dropDatabase() { "ok" : 1 } >
查看數(shù)據(jù)庫版本
> db.version() 4.0.20 >
查看當(dāng)前庫統(tǒng)計(jì)信息
> db.stats() { "db" : "LearnDB", "collections" : 0, "views" : 0, "objects" : 0, "avgObjSize" : 0, "dataSize" : 0, "storageSize" : 0, "numExtents" : 0, "indexes" : 0, "indexSize" : 0, "fileSize" : 0, "fsUsedSize" : 0, "fsTotalSize" : 0, "ok" : 1 } >
2. 集合操作
默認(rèn)情況下,mongo在插入文檔的時(shí)候,集合默認(rèn)會(huì)被創(chuàng)建
2.1 用法
查看當(dāng)前庫的集合
show Collections
插入單條數(shù)據(jù)
db.CollectionName.insertOne()
插入多條數(shù)據(jù)
db.CollectionName.insertMany()
刪除集合
db.CollectionName.drop()
修改集合名稱
db.CollectionName.renameCollection()
創(chuàng)建集合
db.createCollection()
2.2 例子
插入單條數(shù)據(jù)
> db.stu_info.insertOne({"std_name":"小明","sex":"男","reg_time":Date()}) { "acknowledged" : true, "insertedId" : ObjectId("61d6b4afa5a7b57ea8e0a9db") }
查看當(dāng)前庫的集合
> show collections stu_info >
插入多條數(shù)據(jù)
> db.stu_info.insertMany([{"std_name":"小剛","sex":"男","reg_time":Date()},{"std_name":"小紅","sex":"女","reg_time":Date()}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("61d6b59da5a7b57ea8e0a9dc"), ObjectId("61d6b59da5a7b57ea8e0a9dd") ] } >
刪除集合
> db.stu_info.drop() true >
修改集合名稱
> db.stu_info.renameCollection("stuInfo") { "ok" : 1 } > show collections stuInfo >
創(chuàng)建集合
默認(rèn)情況下,向集合中插入數(shù)據(jù),會(huì)默認(rèn)創(chuàng)建,還有一種情況,是設(shè)置集合為 容量上限模式 ,則需要提前創(chuàng)建
語法
db.createCollection(
name: 集合名稱 capped: 若為true,則創(chuàng)建 有上限模式 的集合 size: 設(shè)置上限的最大大小,以字節(jié)為單位,若達(dá)到最大值,則會(huì)刪除舊文檔 max: 允許最大文檔數(shù),當(dāng)同時(shí)設(shè)置 size 和 max , 則 size 優(yōu)先級(jí) 大于 max
創(chuàng)建有上限的集合, 允許的條數(shù)為5條, 最大容量為1G
> db.createCollection("std_log",{"capped":true , "size": 1073741824 , "max": 5}) { "ok" : 1 } >
測(cè)試插入數(shù)據(jù)
可以發(fā)現(xiàn),插入超過5條后,新插入的數(shù)據(jù),會(huì)刪除舊文檔,然后插入新文檔
> db.std_log.insertMany([{"id":1,"status":true,"time":Date()},{"id":2,"status":false,"time":Date()},{"id":3,"status":true,"time":Date()},{"id":4,"status":true,"time":Date()},{"id":5,"status":false,"time":Date()}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("61d6c44da5a7b57ea8e0aa01"), ObjectId("61d6c44da5a7b57ea8e0aa02"), ObjectId("61d6c44da5a7b57ea8e0aa03"), ObjectId("61d6c44da5a7b57ea8e0aa04"), ObjectId("61d6c44da5a7b57ea8e0aa05") ] } > db.std_log.find() { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa01"), "id" : 1, "status" : true, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa02"), "id" : 2, "status" : false, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa03"), "id" : 3, "status" : true, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa04"), "id" : 4, "status" : true, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa05"), "id" : 5, "status" : false, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } > > db.std_log.insertOne({"id":6,"status":false,"time":Date()}) { "acknowledged" : true, "insertedId" : ObjectId("61d6c4b9a5a7b57ea8e0aa07") } > db.std_log.find() { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa02"), "id" : 2, "status" : false, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa03"), "id" : 3, "status" : true, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa04"), "id" : 4, "status" : true, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c44da5a7b57ea8e0aa05"), "id" : 5, "status" : false, "time" : "Thu Jan 06 2022 18:28:29 GMT+0800 (CST)" } { "_id" : ObjectId("61d6c4b9a5a7b57ea8e0aa07"), "id" : 6, "status" : false, "time" : "Thu Jan 06 2022 18:30:17 GMT+0800 (CST)" } >
3. 查詢操作
3.1 用法
查詢集合文檔條數(shù)
db.CollectionNmae.count()
普通查詢
db.CollectionName.find()
格式化顯示
db.CollectionName.find().pretty()
比較查詢
等于
db.CollectionName.find({k:v})
不等于
db.CollectionName.find({k:{$ne: v})
大于
db.CollectionName.find({k:{$gt: v})
小于
db.CollectionName.find({k:{$lt: v})
大于或等于
db.CollectionName.find({k:{$lte: v})
小于或等于
db.CollectionName.find({k:{$gte: v})
and | or 查詢
and
db.CollectionName.find({k:v,k1:v1})
or
db.CollectionName.find({$or:[{k1:v1},{k2:v2}]})
and 和 or 聯(lián)合查詢
db.CollectionName.find({k:v,$or:[{k1:v1},{k2:v2}]})
排序查詢
db.CollectionName.find().sort({k:1})
指定返回行數(shù)查詢
db.CollectionName.find().limit()
db.CollectionName.find().skip()
指定返回域
db.CollectionName.find({k:v},{k1:1,k2:1})
只返回一條
db.CollectionName.findOne({k:v})
只返回一條并且刪除該數(shù)據(jù)
db.CollectionName.findOneAndDelete({k:v})
3.2 例子
數(shù)據(jù)準(zhǔn)備
> use LearnDB switched to db LearnDB > > db.dropDatabase() { "dropped" : "LearnDB", "ok" : 1 } > > > db.stu_info.insertMany([ ... {"name": "小明" , "sex": "男" , "score1": 98 , "score2": 60, "score3": 79 , "score5": 101 , "classID: " : 1}, ... {"name": "小紅" , "sex": "女" , "score1": 100 , "score2": 100, "score3": 100 , "score5": 80 , "classID: " : 3}, ... {"name": "小趙" , "sex": "女" , "score1": 80 , "score2": 79, "score3": 33 , "score5": 55 , "classID: " : 3}, ... {"name": "小錢" , "sex": "男" , "score1": 89 , "score2": 36, "score3": 102 , "score5": 65 , "classID: " : 5}, ... {"name": "小李" , "sex": "女" , "score1": 53 , "score2": 91, "score3": 34 , "score5": 19 , "classID: " : 9}, ... {"name": "小王" , "sex": "女" , "score1": 100 , "score2": 100, "score3": 100 , "score5": 99 , "classID: " : 1}, ... {"name": "小周" , "sex": "女" , "score1": 99 , "score2": 94, "score3": 92 , "score5": 90 , "classID: " : 6} ... ]) { "acknowledged" : true, "insertedIds" : [ ObjectId("61d6cac2a5a7b57ea8e0aa08"), ObjectId("61d6cac2a5a7b57ea8e0aa09"), ObjectId("61d6cac2a5a7b57ea8e0aa0a"), ObjectId("61d6cac2a5a7b57ea8e0aa0b"), ObjectId("61d6cac2a5a7b57ea8e0aa0c"), ObjectId("61d6cac2a5a7b57ea8e0aa0d"), ObjectId("61d6cac2a5a7b57ea8e0aa0e") ] } >
查詢集合文檔條數(shù)
> db.stu_info.count() 7 >
普通查詢
mongo shell 默認(rèn)只返回10條,超過10條 需要 根據(jù)提示往下翻頁
> db.stu_info.find() { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "sex" : "女", "score1" : 80, "score2" : 79, "score3" : 33, "score5" : 55, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } >
指定返回行數(shù)查詢
查詢2個(gè)文檔
> db.stu_info.find().limit(2) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } >
跳過第1個(gè)文檔,查詢1個(gè)文檔
> db.stu_info.find().skip(1).limit(1) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } >
格式化顯示
> db.stu_info.find().pretty().limit(2) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } >
比較查詢
等于: 查詢 name 等于 小明 的文檔
> db.stu_info.find({"name":"小明"}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } >
不等于: 查詢 name 不等于 小明 的文檔,并且取出第一個(gè)
> db.stu_info.find({"name":{$ne:"小明"}}).limit(1).pretty() { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } >
大于: 查詢 socre3 大于100 的文檔
> db.stu_info.find({"score3":{$gt:100}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } >
小于: 查詢 socre1 小于80 的文檔
> db.stu_info.find({"score1":{$lt:80}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } >
大于或等于:查詢 socre3 大于等于 100 的文檔
> db.stu_info.find({"score3":{$gte:100}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } >
大于或等于:查詢 socre1 小于等于 80 的文檔
> db.stu_info.find({"score1":{$lte:80}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "sex" : "女", "score1" : 80, "score2" : 79, "score3" : 33, "score5" : 55, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } >
and | or 查詢
and:查詢 score1等于100 score2等于100 的文檔
> db.stu_info.find({"score1":100,"score2":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } >
**or: 查詢socre1 等于 53 或者score2等于 94 的文檔 **
> db.stu_info.find({$or:[{"score1":53},{"score2":94}]}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } >
and 和 or 聯(lián)合查詢 :查詢sex 等于 男 ,在此基礎(chǔ)上 查詢 score1 等于 99 或者 score1 等于 91 的文檔
> db.stu_info.find({"sex":"女",$or:[{"score1":99},{"score2":91}]}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } >
排序查詢: 對(duì)score1進(jìn)行升序和降序排序
1: 升序
-1: 降序
> db.stu_info.find().sort({"score1":1}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "sex" : "女", "score1" : 80, "score2" : 79, "score3" : 33, "score5" : 55, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } >
> db.stu_info.find().sort({"score1":-1}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "sex" : "女", "score1" : 80, "score2" : 79, "score3" : 33, "score5" : 55, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } >
指定返回域: 獲取name 和 score1 域的數(shù)據(jù)
> db.stu_info.find({},{"name":1,"score1":1}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "score1" : 98 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "score1" : 100 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "score1" : 80 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "score1" : 89 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "score1" : 53 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "score1" : 100 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "score1" : 99 } >
只返回一條:查詢socre1小于等于100的數(shù)據(jù),僅返回一條
> db.stu_info.findOne({"score1": {$lte: 100}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } >
只返回一條并且刪除該數(shù)據(jù):查詢socre1小于等于100的數(shù)據(jù),僅返回一條,并且刪除
> db.stu_info.findOneAndDelete({"score1":{$lte:100}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa08"), "name" : "小明", "sex" : "男", "score1" : 98, "score2" : 60, "score3" : 79, "score5" : 101, "classID: " : 1 } > db.stu_info.findOne({"name":"小明"}) null >
4. 更新操作
4.1 用法
更新一個(gè)文檔
db.CollectionName.updateOne()
更新多個(gè)文檔
db.CollectionName.updateOne()
db.CollectionName.updateMany()
4.2 例子
只更新一個(gè)文檔:將 score1 為 100 的文檔 新增一個(gè)classID 為 16
> db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } > > db.stu_info.updateOne({"score1":100},{$set:{"classID":16}}) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } > > db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3, "classID" : 16 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } >
更新多個(gè)文檔
更新所有文檔:將 score1 為 100 的文檔 設(shè)置 新增/設(shè)置一個(gè) classID 為 21
> db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3, "classID" : 16 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } > > db.stu_info.updateMany({"score1":100},{$set:{"classID":21}}) { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 } > > db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3, "classID" : 21 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1, "classID" : 21 } >
5. 刪除操作
5.1 用法
刪除一條文檔
db.CollectionName.deleteOne()
刪除多條文檔
db.CollectionName.deleteMany()
5.2 例子
刪除一條文檔:刪除socre1為100的一條文檔
> db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa09"), "name" : "小紅", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 80, "classID: " : 3, "classID" : 21 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1, "classID" : 21 } > > db.stu_info.deleteOne({"score1":100}) { "acknowledged" : true, "deletedCount" : 1 } > > db.stu_info.find({"score1":100}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1, "classID" : 21 } >
刪除多條文檔: 刪除socre1小于90的所有文檔
> db.stu_info.find({"score1":{$lt:90}}) { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0a"), "name" : "小趙", "sex" : "女", "score1" : 80, "score2" : 79, "score3" : 33, "score5" : 55, "classID: " : 3 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0b"), "name" : "小錢", "sex" : "男", "score1" : 89, "score2" : 36, "score3" : 102, "score5" : 65, "classID: " : 5 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0c"), "name" : "小李", "sex" : "女", "score1" : 53, "score2" : 91, "score3" : 34, "score5" : 19, "classID: " : 9 } > > db.stu_info.deleteMany({"score1":{$lt:90}}); { "acknowledged" : true, "deletedCount" : 3 } > > db.stu_info.find({"score1":{$lt:90}}) >
6. 索引操作
6.1 用法
查詢集合所有
db.CollectionName.getIndexes()
創(chuàng)建索引
db.CollectionName.createIndex()
刪除集合除_id以外的所有索引
db.CollectionName.dropIndexes()
刪除索引
db.CollectionName.dropIndex()
6.2 例子
查詢集合所有
> db.stu_info.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "LearnDB.stu_info" } ] >
創(chuàng)建索引
語法
db.collection.createIndex( { "a": 1 }, { unique: true, sparse: true, expireAfterSeconds: 3600 } )
可選參數(shù)
background: bool類型 為true則在后臺(tái)構(gòu)建索引,默認(rèn)為false unique: bool類型 為true則創(chuàng)建唯一索引,默認(rèn)為false name: string類型 索引名稱, 如果未指定,mongodb將通過索引字段和排序來生成索引名稱 sparse: bool類型 為true則對(duì)文檔不存在字段數(shù)據(jù)不啟用索引,默認(rèn)為false expireAfterSeconds: 整形 指定一個(gè)值(以秒為單位)作為TTL,設(shè)置生存時(shí)間
例子1: 給集合stu_info sex name 增加唯一索引
> db.stu_info.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "LearnDB.stu_info" } ] > > db.stu_info.createIndex({"name":1} , {"background":true,"unique":true , "name":"stu_info_name"}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > > db.stu_info.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "LearnDB.stu_info" }, { "v" : 2, "unique" : true, "key" : { "name" : 1 }, "name" : "stu_info_name", "ns" : "LearnDB.stu_info", "background" : true } ] >
嘗試新增相同name的文檔
> db.stu_info.find() { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0d"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1, "classID" : 21 } { "_id" : ObjectId("61d6cac2a5a7b57ea8e0aa0e"), "name" : "小周", "sex" : "女", "score1" : 99, "score2" : 94, "score3" : 92, "score5" : 90, "classID: " : 6 } > > db.stu_info.insertMany([ {"name": "小王" , "sex": "女" , "score1": 100 , "score2": 100, "score3": 100 , "score5": 99 , "classID: " : 1} ]) 2022-01-08T10:56:46.475+0800 E QUERY [js] BulkWriteError: write error at item 0 in bulk operation : BulkWriteError({ "writeErrors" : [ { "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: LearnDB.stu_info index: stu_info_name dup key: { : \"小王\" }", "op" : { "_id" : ObjectId("61d8fd6e3a0069c72ccea003"), "name" : "小王", "sex" : "女", "score1" : 100, "score2" : 100, "score3" : 100, "score5" : 99, "classID: " : 1 } } ], "writeConcernErrors" : [ ], "nInserted" : 0, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) BulkWriteError@src/mongo/shell/bulk_api.js:369:48 BulkWriteResult/this.toError@src/mongo/shell/bulk_api.js:333:24 Bulk/this.execute@src/mongo/shell/bulk_api.js:1173:1 DBCollection.prototype.insertMany@src/mongo/shell/crud_api.js:314:5 @(shell):1:1 >
刪除索引:刪除stu_info name 索引
> db.stu_info.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "LearnDB.stu_info" }, { "v" : 2, "unique" : true, "key" : { "name" : 1 }, "name" : "stu_info_name", "ns" : "LearnDB.stu_info", "background" : true } ] > > db.stu_info.dropIndex("stu_info_name") { "nIndexesWas" : 2, "ok" : 1 } > > db.stu_info.getIndexes() [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "LearnDB.stu_info" } ] >
完
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。