robot 3T 操作MongoDB數據庫常用命令

名稱    描述
db.collection.aggregate()    提供對聚合管道的訪問。
db.collection.bulkWrite()    提供批量寫入操作功能。
db.collection.copyTo()    已過時。包裝eval在單個MongoDB實例中的集合之間複製數據。
db.collection.count()    Wraps count返回集合或視圖中文檔數的計數。
db.collection.countDocuments()    $group使用$sum 表達式包裝聚合階段以返回集合或視圖中文檔數的計數。
db.collection.estimatedDocumentCount()    count用於返回集合或視圖中文檔的近似計數的包裝。
db.collection.createIndex()    在集合上構建索引。
db.collection.createIndexes()    在集合上構建一個或多個索引。
db.collection.dataSize()    返回集合的大小。包裹size輸出中的字段collStats。
db.collection.deleteOne()    刪除集合中的單個文檔。
db.collection.deleteMany()    刪除集合中的多個文檔。
db.collection.distinct()    返回具有指定字段的不同值的文檔數組。
db.collection.drop()    從數據庫中刪除指定的集合。
db.collection.dropIndex()    刪除集合上的指定索引。
db.collection.dropIndexes()    刪除集合上的所有索引。
db.collection.ensureIndex()    已過時。使用db.collection.createIndex()。
db.collection.explain()    返回有關各種方法的查詢執行的信息。
db.collection.find()    對集合或視圖執行查詢並返回遊標對象。
db.collection.findAndModify()    以原子方式修改並返回單個文檔。
db.collection.findOne()    執行查詢並返回單個文檔。
db.collection.findOneAndDelete()    查找單個文檔並將其刪除。
db.collection.findOneAndReplace()    查找單個文檔並替換它。
db.collection.findOneAndUpdate()    查找單個文檔並進行更新。
db.collection.getIndexes()    返回描述集合上現有索引的文檔數組。
db.collection.getShardDistribution()    對於分片羣集中的集合,db.collection.getShardDistribution()報告塊分佈的數據。
db.collection.getShardVersion()    分片羣集的內部診斷方法。
db.collection.group()    已過時。提供簡單的數據聚合功能。通過鍵對集合中的文檔進行分組,並處理結果。使用aggregate()了更復雜的數據集合。
db.collection.insert()    在集合中創建新文檔。
db.collection.insertOne()    在集合中插入新文檔。
db.collection.insertMany()    在集合中插入幾個新文檔。
db.collection.isCapped()    報告集合是否爲上限集合。
db.collection.latencyStats()    返回集合的延遲統計信息。
db.collection.mapReduce()    執行map-reduce樣式數據聚合。
db.collection.reIndex()    重建集合上的所有現有索引。
db.collection.remove()    從集合中刪除文檔。
db.collection.renameCollection()    更改集合的名稱。
db.collection.replaceOne()    替換集合中的單個文檔。
db.collection.save()    提供圍繞insert()和update()插入新文檔的包裝器。
db.collection.stats()    報告集合的狀態。提供圍繞的包裝collStats。
db.collection.storageSize()    報告集合使用的總大小(以字節爲單位)。提供輸出storageSize字段周圍的包裝器collStats。
db.collection.totalIndexSize()    報告集合上索引使用的總大小。提供輸出totalIndexSize字段周圍的包裝器collStats。
db.collection.totalSize()    報告集合的總大小,包括所有文檔的大小和集合上的所有索引。
db.collection.update()    修改集合中的文檔。
db.collection.updateOne()    修改集合中的單個文檔。
db.collection.updateMany()    修改集合中的多個文檔。
db.collection.watch()    在集合上建立變更流。
db.collection.validate()    對集合執行診斷操作。

二、常用操作
1.基本狀態查看:
db.getCollection('集合名').stats()  

此方法爲查看文檔的一些統計信息。

2.常規查詢:
db.getCollection('集合名').find({'字段名':'字段屬性'})

3.查找某個字段不存在的文檔:
db.getCollection('集合名').find({'字段名':{$exists:false}})

4.多字段查詢:
db.getCollection('集合名').find({'字段1':{$exists:false},'字段2':{$exists:true}}).count()

5.嵌套字段的操作:
例如:字段name是嵌套在people下的字段,即name是people的子字段。

查找所有name爲“lucy”的文檔,則在people和name之間加點"."表示。

db.getCollection('集合名').find({'people.name':‘lucy’})

6.查找大於(大於,小於,等方法)某個值得文檔
db.getCollection('集合名').find({'字段名':{'$gt':數值}})

$gt:大於;    $lt:小於;    $gte:大於或等於;    $lte:小於或等於; $ne: 不等於

注:使用不等於時,"$ne"後面可以跟非數值型的數據,例如str類型。

例如 查詢字段name存在且不爲空字符串:db.getCollection("集合名").find({"name":{"$exists":true, "$ne":""}})

7.刪除指定字段:
db.getCollection('集合名').update({'字段名':{$exists:true}}, {$unset:{'字段名':''}}, {multi:true})

參數multi設置爲true表示對集合中的所有文檔執行該命令,若設置爲false則只修改找到的第一條文檔。

8.刪除滿足某條件的文檔:
db.getCollection('集合名').remove({'字段名':'條件'})

此處的“條件”同find命令的查詢條件。

例如:db.getCollection('API').remove({'created':{'$gt':154, '$lt':156}}), 爲刪除集合“API”中‘created’字段屬性在154-156之間的數據。

9.update更新字段屬性:
db.getCollection('集合名').update({'字段名':‘原屬性’},{'$set':{'字段名':‘目標屬性’}},{multi:true})

10.按照指定排序輸出顯示:
db.getCollection('集合名').find().sort({"字段名":-1}) 

其中 1 爲升序排列,而 -1 是用於降序排列

11.只輸出顯示某個字段:
db.getCollection('集合名').find({}, {'要顯示的字段':1})

12.查看集合索引:
db.getCollection('集合名').getIndexes()

13.使用正則匹配查詢某個字段中含有“某部分”內容的文檔(部分匹配):
db.getCollection('集合名').find({post_text:{$regex:"runoob"}})

按鍵盤F5或者Ctrl+Enter(回車)執行操作。
 

發佈了27 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章