mongodb基本命令和CURD操作

來源:互聯網
上載者:User

一,基本命令:

1,查看所有資料庫:show dbs                 

2,選擇資料庫  use dbName,如果資料庫不存在,則會建立資料庫 

3,查看所有資料庫相關的操作(類似javascript函數) db.help()

使用db.help()得到資料庫所有操作如下: > db.help()
DB methods:
        db.addUser(userDocument)
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.r
unCommand(...) ]
        db.auth(username, password)
        db.cloneDatabase(fromhost) copy database
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } ) 建立集合
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase() drop database
        db.eval(func, args) run code server-side
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionNames() 得到資料庫中所有集合的名字
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName() 得到資料庫名字
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats() 顯示所有集合資訊
        db.printReplicationInfo()
        db.printShardingStatus() 查看分區資訊
        db.printSlaveReplicationInfo()
        db.removeUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj
 : 1 }
        db.serverStatus() 查看伺服器資訊
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats() 查看資料庫相關資訊
        db.version() current version of the server
>


4,刪除資料庫  db.dropDatabase()

5,查看所有集合  show collections

6,建立集合   db.collectionName.insert({_id:123})  當向不存在的集合中插入對象時,會建立集合對象  也可以使用db.createCollection("users")建立集合

7,刪除集合  db.collectionName.drop()  刪除集合

8,顯示集合所有對象: db.collectionName.find()

9, 刪除集合所有對象: db.collectionName.remove()  只是刪除集合中的所有資料,但是集合和相應的索引都還是存在的

二 mongdb  的CURD 相關操作

1,添加操作

1.1,單個添加:

> use mogo
switched to db mogo
> db.users.insert({_id:123,name:"123"})
> db.users.find()
{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

switched to db mogo

> db.users.insert({_id:123,name:"123"})

> db.users.find()

{ "_id" : 123, "name" : "123" }

1.2 ,大量新增(使用javascript for 迴圈添加):

> for(var i=0;i<5;i++){

... db.users.insert({

... _id:i,name:"name"+i,sex:20+i,address:"address"+i,tell:"134567890"+i}

... )}

> db.users.find()

{ "_id" : 123, "name" : "123" }

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

>  

1.3,insert() 和save():如果使用insert()插入的時候_id 已經存在會報錯,而如果使用save()則會修改資訊

> db.users.find()

{ "_id" : 123, "name" : "123" } 修改前的值

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.insert({_id:123,name:"12345"})

E11000 duplicate key error index: mogo.users.$_id_  dup key: { : 123.0 } 主鍵重複錯誤

> db.users.save({_id:123,name:"12345"})

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : 123, "name" : "12345" } save()方法修改之後的值

2,刪除操作  

2.1:條件刪除 db.collectionNmae.remove({})        {}放刪除的條件         

> db.users.find()  

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

{ "_id" : 123, "age" : "12345" }

> db.users.remove({_id:123})  //根據id刪除

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

>

         2.2:全部刪除db.collectionName.remove()


3,修改操作

3.1  :update 操作 db.collectionName.update({修改條件},{修改後的值})

> db.users.find()

{ "_id" : 0, "name" : "name0", "sex" : 20, "address" : "address0", "tell" : "1345678900" }

{ "_id" : 1, "name" : "name1", "sex" : 21, "address" : "address1", "tell" : "1345678901" }

{ "_id" : 2, "name" : "name2", "sex" : 22, "address" : "address2", "tell" : "1345678902" }

{ "_id" : 3, "name" : "name3", "sex" : 23, "address" : "address3", "tell" : "1345678903" }

{ "_id" : 4, "name" : "name4", "sex" : 24, "address" : "address4", "tell" : "1345678904" }

> db.users.update(

... {name:"name0"}, //修改條件

... {name:"name00"}) //修改後的值

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.