1. 程式人生 > >python學習之數據庫:mongoDB

python學習之數據庫:mongoDB

list 地址 col ica import 狀態 end 最大 sre

參考網址:

1、http://www.runoob.com/mongodb/mongodb-intro.html

2、http://www.runoob.com/python/python-mysql.html

3、http://www.cnblogs.com/descusr/archive/2011/11/15/2249391.html

from pymongo import * # 導包 con = Connection(...) # 鏈接 db = con.database # 鏈接數據庫 db.authenticate(‘username‘, ‘password‘) # 登錄 db.drop_collection(‘users‘) #刪除表 db.logout() # 退出 db.collection_names() # 查看所有表 db.users.count() # 查詢數量 db.users.find_one({‘name‘ : ‘xiaoming‘}) # 單個對象 db.users.find({‘age‘ : 18}) # 所有對象 db.users.find({‘id‘:64}, {‘age‘:1,‘_id‘:0}) # 返回一些字段 默認_id總是返回的 0不返回 1返回 db.users.find({}).sort({‘age‘: 1}) # 排序 db.users.find({}).skip(2).limit(5) # 切片

db.users.find({}, {}, 10, 20) # 第二種寫法 切片 未測試

collection

屬性:full_name,name,database insert(self, doc_or_docs, manipulate=True, safe=False, check_keys=True, **kwargs):
  • 當manipulate=True 時,_id 會被自動添加到 dict,並返回些 _id(or list(_id)),
    為False 時,返回 None
  • 當 safe 為 True 時,會自動判斷to_save 是否為 dict,如果檢查出錯,會提示出錯信息,會等待數據庫的響應,而不會執行插入數據庫
  • doc_or_docs 允許插入一條或多條記錄
save(self, to_save, manipulate=True, safe=False, **kwargs)
  • to_save,只接受一個 dict (字典)
  • 當 to_save 裏包含 _id 時,為update (可能update 多條)
drop(self)
  • 刪除些 collection (集合及數據一起刪除)
remove(self, spec_or_id=None, safe=False, **kwargs)
  • romve 一條記錄
  • 刪除操作不可還原
  • 當 safe 為 True 時,會自動判斷參數的正確性,如果有異常,會等待數據庫的響應,而不會執行刪除操作
  • spec_or_id ,為一完整doc 或指定了 _id 的字典
find_one(self, spec_or_id=None, *args, **kwargs)
  • spec_or_id ,為一完整doc 或指定了 _id 的字典
  • 返回一條符合條件的記錄
find(self, *args, **kwargs)
  • 參數必需是一個 doc 原型(eg:{“name”:"abc"})
  • 返回 Cursor ,並非(result collection)
  • spec 查詢條件
  • fields 返回的字段清單,但_id 為默認返回
  • skip 省略的記錄數
  • limit 一次返回的最大記錄數
  • timeout 如果為True ,超時會自動斷開
  • snapshot 快照模式,True 時保證返回結果不重復
  • tailable 保持tailable 遊標的開啟,並記錄上次的最後位
  • sort 排序字段
  • max_scan 允許查詢的最大數量 ( version **>= 1.5.1**)
  • as_class 指定查詢結果的類型,默認為 ~pymongo.connection.Connection.document_class
  • network_timeout 指定超時時間
count(self) 返回總記錄數 create_index(self, key_or_list, deprecated_unique=None, ttl=300, **kwargs)
  • 為一個或多個key創建索引
  • key 必須為字符串類型
  • eg: create_index("mike")
  • eg: create_index([("mike", pymongo.DESCENDING), ("eliot", pymongo.ASCENDING)])
ensure_index(self, key_or_list, deprecated_unique=None, ttl=300, **kwargs)
  • 檢查是否存在些索引,如果不存在則創建,存在則返回None
drop_indexes(self) 刪除所以索引 drop_index(self, index_or_name) 刪除指定索引 index_information(self) 返回索引信息 rename(self, new_name, **kwargs) 重新指向一個collection distinct(self, key) 返回所有key 不重復的記錄

01 mongo –path

02 db.AddUser(username,password) 添加用戶

03 db.auth(usrename,password) 設置數據庫連接驗證

04 db.cloneDataBase(fromhost) 從目標服務器克隆一個數據庫

05 db.commandHelp(name) returns the help for the command

06 db.copyDatabase(fromdb,todb,fromhost) 復制數據庫fromdb—源數據庫名稱,todb—目標數據庫名稱,fromhost—源數據庫服務器地址

07 db.createCollection(name,{size:3333,capped:333,max:88888}) 創建一個數據集,相當於一個表

08 db.currentOp() 取消當前庫的當前操作

09 db.dropDataBase() 刪除當前數據庫

10 db.eval(func,args) run code server-side

11 db.getCollection(cname) 取得一個數據集合,同用法:db[‘cname‘] or db.cname

12 db.getCollenctionNames() 取得所有數據集合的名稱列表

13 db.getLastError() 返回最後一個錯誤的提示消息

14 db.getLastErrorObj() 返回最後一個錯誤的對象

15 db.getMongo() 取得當前服務器的連接對象get the server connection object

16 db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair

17 db.getName() 返回當操作數據庫的名稱

18 db.getPrevError() 返回上一個錯誤對象

19 db.getProfilingLevel() ?什麽等級

20 db.getReplicationInfo() ?什麽信息

21 db.getSisterDB(name) get the db at the same server as this onew

22 db.killOp() 停止(殺死)在當前庫的當前操作

23 db.printCollectionStats() 返回當前庫的數據集狀態

24 db.printReplicationInfo()

25 db.printSlaveReplicationInfo()

26 db.printShardingStatus() 返回當前數據庫是否為共享數據庫

27 db.removeUser(username) 刪除用戶

28 db.repairDatabase() 修復當前數據庫

29 db.resetError()

30 db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}

31 db.setProfilingLevel(level) 0=off,1=slow,2=all

32 db.shutdownServer() 關閉當前服務程序

33 db.version() 返回當前程序的版本信息

34

35 db.linlin.find({id:10}) 返回linlin數據集ID=10的數據集

36 db.linlin.find({id:10}).count() 返回linlin數據集ID=10的數據總數

37 db.linlin.find({id:10}).limit(2)返回linlin數據集ID=10的數據集從第二條開始的數據集

38 db.linlin.find({id:10}).skip(8) 返回linlin數據集ID=10的數據集從0到第八條的數據集

39 db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數據集ID=1=的數據集從第二條到第八條的數據

40 db.linlin.find({id:10}).sort() 返回linlin數據集ID=10的排序數據集

41 db.linlin.findOne([query]) 返回符合條件的一條數據

42 db.linlin.getDB() 返回此數據集所屬的數據庫名稱

43 db.linlin.getIndexes() 返回些數據集的索引信息

44 db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

45 db.linlin.mapReduce(mayFunction,reduceFunction,

46 )

47 db.linlin.remove(query) 在數據集中刪除一條數據

48 db.linlin.renameCollection(newName) 重命名些數據集名稱

49 db.linlin.save(obj) 往數據集中插入一條數據

50 db.linlin.stats() 返回此數據集的狀態

51 db.linlin.storageSize() 返回此數據集的存儲大小

52 db.linlin.totalIndexSize() 返回此數據集的索引文件大小

53 db.linlin.totalSize() 返回些數據集的總大小

54 db.linlin.update(query,object[,upsert_bool])在此數據集中更新一條數據

55 db.linlin.validate() 驗證此數據集

56 db.linlin.getShardVersion() 返回數據集共享版本號

57 db.linlin.find({‘name’:‘foobar’}) select * from linlin where name=’foobar’

58 db.linlin.find() select * from linlin

59 db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10

60 db.linlin.find().skip(10).limit(20) 從查詢結果的第十條開始讀20條數據 select * from linlin limit 10,20 ———-mysql

61 db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)

62 db.linlin.find().sort({‘ID’:-1}) select * from linlin order by ID desc

63 db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20

64 db.linlin.group({key:{‘name‘:true},cond:{‘name‘:‘foo‘},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

65 select name,sum(marks) from linlin group by name

66 db.linlin.find(‘this.ID<20′,{name:1}) select name from linlin where ID<20

67 db.linlin.insert({‘name‘:‘foobar’,‘age‘:25}) insert into linlin (‘name‘,‘age’)values(‘foobar‘,25)

68 db.linlin.insert({‘name‘:‘foobar’,‘age‘:25,’email’:‘[email protected]’})

69 db.linlin.remove({}) delete * from linlin

70 db.linlin.remove({‘age‘:20}) delete linlin where age=20

71 db.linlin.remove({‘age‘:{$lt:20}}) delete linlin where age<20

72 db.linlin.remove({‘age‘:{$lte:20}}) delete linlin where age<=20

73 db.linlin.remove({‘age‘:{$gt:20}}) delete linlin where age>20

74 db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20

75 db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20

76 db.linlin.update({‘name’:‘foobar’},{‘$set’:{‘age’:36}}) update linlin set age=36 where name=’foobar’

77 db.linlin.update({‘name’:‘foobar’},{‘$inc’:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’

python學習之數據庫:mongoDB