mongoDB資料庫基本操作

來源:互聯網
上載者:User

標籤:mongodb

mongoDB中聚集集合(collection/coll)相當於mysql中的表

文檔相當於mysql中的每一行資料


常用命令

 1.獲得協助命令

db.help()db.表名.help()db.表名.find().help()rs.help()

 2.切換/建立資料庫

use 資料庫名稱  注意:mongoDB的資料庫可以先使用,後建立,當建立一個集合(table)時,當前資料庫會自動建立例子:有一個資料庫為sutdentdb,要切換到這個資料庫則:use studentdb

 3.查詢所有資料庫

show dbs例子:> show dbsadmin      0.078GBlocal      0.078GBstudentdb  0.078GBtest       0.078GB

 4.刪除當前使用的資料庫

db.dropDatabase()例子:> use testswitched to db test> db.dropDatabase(){ "dropped" : "test", "ok" : 1 }> show dbsadmin      0.078GBlocal      0.078GBstudentdb  0.078GB

 5.查看當前使用的資料庫

db.getName()例子:> use studentdbswitched to db studentdb> db.getName()studentdb

 6.顯示當前db狀態

db.stats()

 7.當前db版本

> db.version()2.6.12


查看聚集集合(表)的基本資料

  1. 查看協助

db.表名.help()

2.查詢當前集合中的資料條數

db.表名.count()

3.查看資料空間大小

db.表名.dataSize()

4.當前聚集集合所在的資料庫

db.表名.getDB()

5.得到當前聚集集合的狀態

db.表名.stats()

6.得到聚集集合的總大小

db.表名.totalSize()

7.查看聚集集合儲存空間大小

db.表名.storageSize()

8.查看Shard版本資訊

db.表名.getShardVersion()

9.給聚集集合重新命名

db.表名.renameCollection("新名字")

10.刪除當前聚集集合

db.表名.drop()


查詢操作

1.查詢所有記錄

db.表名.find()相當於:select* from 表名;預設每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁資料。注意:鍵入it命令不能帶“;”

2.查詢結果會過濾掉某一欄位中重複的資料,然後顯示出來

db.表名.distinct("欄位名")例子:> db.student.distinct("Age")[ 20, 40 ]  註:將student表中Age欄位中的資料去重後,顯示出來

3.查詢age=22的資料

db.表名.find({"age":22})例子:> db.student.find({"age":20}){ "_id" : ObjectId("5740dcb29bc83e10802f1cd8"), "name" : "kity", "age" : 20, "gender" : "womand" }

4.查詢age>22的資料

大於符號  $gt> db.student.find({"age":{$gt:22}}){ "_id" : ObjectId("5740c1419bc83e10802f1cd6"), "name" : "Tom", "age" : 23, "gender" : "male" }{ "_id" : ObjectId("5740dc369bc83e10802f1cd7"), "name" : "jerry", "age" : 30, "gender" : "male", "guoji" : "china" }{ "_id" : ObjectId("5742263773344acd707888ba"), "name" : "Gu Zenghui", "age" : 25, "gender" : "male", "Course" : "Linux" }

5.

mongoDB資料庫基本操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.