Mongo客戶端常用命令 一、數據庫相關命令 1 show dbs // 列出所有數據庫 2 use memo // 使用數據庫memo。即使這個數據庫不存在也可以執行,但該數據庫不會立刻被新建,要等到執行了insert之類的操作時,才會建立這個數據庫 www.2cto.com 3 show collections
Mongo客戶端常用命令
一、數據庫相關命令
1 show dbs // 列出所有數據庫
2 use memo // 使用數據庫memo。即使這個數據庫不存在也可以執行,但該數據庫不會立刻被新建,要等到執行了insert之類的操作時,才會建立這個數據庫
www.2cto.com
3 show collections // 列出當前數據庫的collections
4 查看各collection的狀態 db.printCollectionStats()
5 db // 顯示當前數據庫
6 show users // 列出用戶
7 db.system.users.find() // 列出用戶
8 db.removeUser('user1') //刪除用戶
9 db.c1.drop()//刪除集合c1
10 db.dropDatabase()//刪除當前的數據庫 二、安全與認證
1 use shine // 如果要root權限,就用admin庫
www.2cto.com
2 db.addUser("username", "password") // 普通權限,可讀寫
3 db.addUser("username", "password", true) // 只可讀,不可寫
4 db.system.users.remove({user: username}) // 刪除用戶 1 db.c1.save({name:"zhangsan",age:18}) //沒有寫主鍵,系統會自動生成一個主鍵,主鍵名為_id, 2 .每個MongoDB的document都有一個_id字段作為它的第一個屬性,這個值通常是一個BSON對象id,因此,這個id對于集合中的每個成員 都是唯一的,如果用戶插入一個document沒有提供一個id,數據庫將自動生成一個id,并存儲在_id字段。
3. db.c1.save({_id:1,name:"lisi",age:22})//自己填寫id主鍵值,id主鍵值可以是字符串類型,也可以是數字類型
4. db.c1.save({"name" : "MongoDB","type" : "database","count" : 1,"info" : {x : 203,y : 102}}) 四、創建索引:
1 coll.ensureIndex({productid:1}) // 在productid上建立普通索引
2 coll.ensureIndex({district:1, plate:1}) // 多字段索引
3 coll.ensureIndex({productid:1}, {unique:true}) // 唯一索引
4 coll.ensureIndex({productid:1}, {unique:true, dropDups:true|) // 建索引時,如果遇到索引字段值已經出現過的情況,則刪除重復記錄
5 coll.getIndexes() // 查看索引
6 coll.dropIndex({productid:1}) // 刪除單個索引 1. db.coll.find() // select * from coll
2. db.coll.find().limit(10) // select * from coll limit 10
3. db.coll.find().sort({x:1}) // select * from coll order by x asc
4. db.coll.find().sort({x:-1}) // select * from coll order by x desc
5. db.coll.find().sort({x:1}).skip(5).limit(10) // select * from coll order by x asc limit 5, 10
6. db.coll.find({x:10}) // select * from coll where x = 10
7. db.coll.find({x: {$lt:10}}) // select * from coll where x <= 10
8. db.coll.find({}, {y:true}) // select y from coll
9. 通過游標訪問數據
.> var cursor = db.collect1.find();
> while(cursor.hasNext()) printjson(cursor.next())
當數據超過20行時候,使用it命令查看更多數據 通過forEach
10> db.collect1.find().forEach(printjson)
11. 在mongo shell中,你可以將游標認為是數組
12> var cursor =db.collect1.find();
13.> printjson(cursor[4])
14{ "_id" : ObjectId("4c691e72ed2a47b462dfa806"), "x" : 4, "y" : 3 }
使用這種方式存取需要注意的是,在cursor[4]之前的所有數據都會同時被加載到內存,對于很大的結果集,這樣操作是不恰當的,會導致內存溢出,當查詢巨大數據量大時候,游標應當作為迭代器使用。
www.2cto.com
SQL語句不能做到的,mongo也可以做到
1. coll.find({"address.city":"gz"}) // 搜索嵌套文檔address中city值為gz的記錄
2. coll.find({likes:"math"}) // 搜索數組
3. coll.ensureIndex({"address.city":1}) // 在嵌套文檔的字段上建 1. db.user.update({uid:1},{$set:{age:26}}) //update user set age=26 where uid=1
2. db.user.update({uid:1},{$inc:{age:1}}) //update user set age=age+1 where uid=1 1.db.user.delete({uid:1})//delete user where uid=1
2.db.Position.remove({"id":10}) //delete * from Position where id=10 1. // json或csv格式,每次一個collection
mongoexport -d producttrade -c basic -o /home/data/mongo_backup/producttrade_100504.json
145.3. mongoimport -d producttrade -c basic --drop /home/data/mongo_backup/producttrade_100504.json // 二進制數據格式,常用于備份、還原
2 mongodump -d shine -o /home/data/mongo_backup
3 mongorestore -d shine --drop /home/data/mongo_backup/shine
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com