2013年1月28日 星期一

在python 中 使用mongoDB 像mysql 一樣取出陣列資料

最近遇到的問題, python 中直接直接使用mysql querry 出來的資料可以用陣列的方式取得, 那 mongo呢?
直接來看代碼
...
...
...

accounts = accounts.find_one({'username':'axdc11239@yahoo.com.hk'})
print accounts
print list(accounts)
print list(accounts)[0]


2013年1月27日 星期日

在python中把string當成object.name

因為最近有需要用到,碰巧看到人家python有出現這樣的代碼 getattr(... , ...)
http://blog.csdn.net/wzm112/article/details/6444668


  1. def query():  
  2.     conn = Connection('127.0.0.1',27017)  
  3.     db    = getattr(conn,'dbname')  
  4.     coll   = getattr(db,'collname')  
  5.      
  6.     #使用正则查询  
  7.     import re  
  8.     q      = rs.compile(r'add'#完成正则 r'.*'  ,r'[a-z]+'  
  9.     rst    = coll.find({'field' : { '$regex' : q } })  
  10.     print rst.count()  #打印查询结果  
  11.     #查询方式2  
  12.     rst    = coll.find({'field' : { '$regex' : r '[a-z]+'} })  
  13.     print rst.count()