MongoDB數(shù)據(jù)庫(kù)基礎(chǔ)操作總結(jié)
本文實(shí)例講述了MongoDB數(shù)據(jù)庫(kù)基礎(chǔ)操作。分享給大家供大家參考,具體如下:
1.創(chuàng)建數(shù)據(jù)庫(kù)
>use test
> db.test.insert({"name":1})
- 插入之后才能查到test
2.查看數(shù)據(jù)庫(kù)
>show dbs
3.刪除數(shù)據(jù)庫(kù)
> use test > db.dropDatabase()
4.創(chuàng)建集合
4.1 集合概念
- 集合就是一組文檔,相當(dāng)于多條記錄。
> db.title.insert({"name":"hyx"})
- 插入之后即創(chuàng)建集合
5.查看集合
> show collections
6.刪除集合
>use test >db.title.drop()
7.插入文檔
7.1 文檔概念
- 多個(gè)鍵及其關(guān)聯(lián)的值有序地放置在一起就是文檔。
- 文檔類似于json數(shù)據(jù)
> db.file.insert({name:"huangyuxin",age:11})
8.查看文檔
>db.files.find()
9.變量方式插入文檔
> document=({by:"hyx"})
{ "by" : "hyx" }
> db.file.insert(document)
WriteResult({ "nInserted" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
>
10.同時(shí)插入多條
> var res = db.file.insertMany([{"b": 3}, {'c': 4}])
> res
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5c6e8bba0fc535200b893f2b"),
ObjectId("5c6e8bba0fc535200b893f2c")
]
}
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "huangyuxin", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
>
11.更新文檔
> db.file.update({"name":"huangyuxin"},{$set:{"name":"hyx"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "by" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>
> db.file.save({"_id" : ObjectId("5c6e8b1c0fc535200b893f2a"),"name":"hyx"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>
12.刪除文檔
12.1刪除指定文檔
> db.title.find()
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2b"), "b" : 3 }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
> db.file.remove({"b":3})
WriteResult({ "nRemoved" : 1 })
> db.file.find()
{ "_id" : ObjectId("5c6e8a060fc535200b893f29"), "name" : "hyx", "age" : 11 }
{ "_id" : ObjectId("5c6e8b1c0fc535200b893f2a"), "name" : "hyx" }
{ "_id" : ObjectId("5c6e8bba0fc535200b893f2c"), "c" : 4 }
{ "_id" : ObjectId("5c6e8cdf0fc535200b893f2d"), "name" : "hyx" }
>
12.2刪除全部文檔
>db.file.deleteMany({})
12.3刪除多個(gè)文檔
>db.file.deleteMany({ status : 1 })
- 刪除當(dāng)前庫(kù)所有status 等于 1 的文檔
13.條件表達(dá)式
13.1$gt 大于
- 查詢age 大于 0 的數(shù)據(jù)
> db.title.find({age:{$gt : 0}})
{ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }
>
13.2 $lt 小于
13.3 $gte 大于等于 $lte 小于等于
- 查詢age 大于等于 0 的數(shù)據(jù)
> db.title.find({age:{$gte : 1}})
13.4 大于小于
> db.title.find({age:{$lt:13,$gt:10}})
{ "_id" : ObjectId("5c6f7ded3ea8783bbfb7fd5f"), "age" : 12 }
{ "_id" : ObjectId("5c6f7e833ea8783bbfb7fd60"), "age" : 12 }
>
13.5 $ne 不等于 $eq 等于
14. $type操作符
- $type操作符是基于BSON類型來(lái)檢索集合中匹配的數(shù)據(jù)類型,并返回結(jié)果。

> db.title.find({"name" : {$type : 2}})
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
>
15. limit()
- 查詢指定條數(shù)
> db.title.find().limit(2)
{ "_id" : ObjectId("5c6e89060fc535200b893f27"), "name" : "yx" }
{ "_id" : ObjectId("5c6f7d633ea8783bbfb7fd5e"), "age" : 10 }
>
- 第一個(gè) {} 放 where 條件,為空表示返回集合中所有文檔。
- 第二個(gè) {} 指定那些列顯示和不顯示 (0表示不顯示 1表示顯示)。
> db.title.find({},{"name":1,_id:0}).limit(1)
{ "name" : "yx" }
>
16.skip()
- 跳過(guò)幾條數(shù)據(jù)
- 不要輕易使用Skip來(lái)做查詢,否則數(shù)據(jù)量大了就會(huì)導(dǎo)致性能急劇下降,這是因?yàn)閟kip是一條一條的數(shù)過(guò)來(lái)的,多了自然就慢了。
17.sort()
- 1 為升序排列,而 -1 是用于降序排列。
> db.title.find({},{'age':1,_id:0}).sort({age:1})
{ }
{ "age" : 10 }
{ "age" : 12 }
{ "age" : 12 }
> db.title.find({},{'age':1,_id:0}).sort({age:-1})
{ "age" : 12 }
{ "age" : 12 }
{ "age" : 10 }
{ }
>
18.索引
18.1 創(chuàng)建單個(gè)索引
- 1 為指定按升序創(chuàng)建索引,降序索引指定為 -1
>db.title.createIndex({"age":1})
18.2 創(chuàng)建多個(gè)索引
>db.title.createIndex({"name":1,"age":-1})
18.3 查看索引
>db.col.getIndexes()
18.4 查看索引大小
>db.col.totalIndexSize()
18.5 刪除所有集合索引
>db.col.dropIndexes()
18.6 刪除指定索引
>> db.title.dropIndex({'age':1})
{ "nIndexesWas" : 2, "ok" : 1 }
>
希望本文所述對(duì)大家MongoDB數(shù)據(jù)庫(kù)程序設(shè)計(jì)有所幫助。
相關(guān)文章
利用golang驅(qū)動(dòng)操作MongoDB數(shù)據(jù)庫(kù)的步驟
這篇文章主要給大家介紹了關(guān)于如何利用golang驅(qū)動(dòng)操作MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
mongodb字段值自增長(zhǎng)實(shí)現(xiàn)代碼
這篇文章主要介紹了mongodb字段值自增長(zhǎng)實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
MongoDB按時(shí)間分組操作實(shí)戰(zhàn)
MongoDB支持使用聚合操作來(lái)統(tǒng)計(jì)數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MongoDB按時(shí)間分組操作的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
使用Locust對(duì)MongoDB進(jìn)行負(fù)載測(cè)試的操作步驟
Locust是一款使用Python開(kāi)發(fā)的開(kāi)源性能測(cè)試工具,支持分布式,可在多臺(tái)主機(jī)上對(duì)系統(tǒng)持續(xù)發(fā)送請(qǐng)求,本文給大家介紹了使用Locust對(duì)MongoDB進(jìn)行負(fù)載測(cè)試的操作步驟,文中通過(guò)圖文結(jié)合的方式介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
Mongodb實(shí)現(xiàn)定時(shí)備份與恢復(fù)的方法教程
這篇文章主要給大家介紹了Mongodb實(shí)現(xiàn)定時(shí)備份與恢復(fù)的方法教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
MongoDB如何正確中斷正在創(chuàng)建的索引詳解
這篇文章主要給大家介紹了關(guān)于MongoDB如何正確中斷正在創(chuàng)建的索引的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
深究從MongoDB的ObjectId中獲取時(shí)間信息
MongoDB默認(rèn)使用_id字段作為主鍵,類型為ObjectId。ObjectId的生成有一定的規(guī)則,詳情可以查看這篇文章MongoDB深究之ObjectId2017-03-03
MongoDB的mongo shell常用操作方法及操作腳本筆記
mongo shell即相當(dāng)于SQL語(yǔ)句在關(guān)系型數(shù)據(jù)庫(kù)中的作用,MongoDB使用JavaScript作為shell操作命令,這里我們就來(lái)整理MongoDB的mongo shell常用操作方法及操作腳本筆記2016-07-07

