python mongo

beichenyx 2011-09-11

1.下载pymongo

wget http://pypi.python.org/packages/source/p/pymongo/pymongo-2.0.1.tar.gz#md5=36300a5c3568707f6c5f1692f42ca008

2.tar -zxvf pymongo-2.0.1.tar.gz

3.cd pymongo-2.0.1

4.python setup.py build

在此步,编译时可能出现的异常

--ImportError: No module named setuptools,这时你可以手工去下载setuptools来安装,在shell中执行如下命令(

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

tarzxvfsetuptools-0.6c11.tar.gz

cdsetuptools-0.6c11

pythonsetup.pybuild

python setup.py install

)

--Python.h: No such file or directory,系统默认的python包不是很全,这时,你需要sudo apt-get install python2.7-dev来安装,或者你可以通过aptitude search python|grep dev来查找你所需要的python

5.python setup.py install

pymongo用例:

pymongo文档

例子如下:

#!/usr/bin/python
#coding: utf-8
import pymongo
#host:127.0.0.1
#port:27017
con = pymongo.Connection('127.0.0.1',27017)
db = con.mydb
db.add_user('test','test')
db.authenticate('test','test')
user=db.user
user.drop()
user.save({'id':1,'name':'pymongo'})
user.save({'id':2,'name':'python'})
datas=user.find()
for data in datas:
    print data
data=user.find_one()
print data
user.create_index('id')
user.find().sort('id',pymongo.ASCENDING)
user.find({'id':1}).count
#user.remove({'id':1})
user.update({'id':2},{'$set':{'name':'kaka'}}
#user.find_one({'id':1})
 

相关推荐