一、安装:
python mongodb驱动: pymongo-2.1.1.tar.gz
python 主程序 : Python-2.7.2.tgz
1. 安装python
cd python/
tar -xvf Python-2.7.2.tgz
cd Python-2.7.2
./configure --prefix=$HOME/mongodb/tools/python2.7
make ; make install
2. 安装pymongo
cd python/
tar -xvf pymongo-2.1.1.tar.gz
cd pymongo-2.1.1
python setup.py install
二、命令测试
1. 查询一行:
[aiobs8@aiobs8 python]$ python
Python 2.7.2 (default, Sep 21 2012, 09:54:17)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
>>> conn = Connection('localhost',27017);
>>> db = conn.db
>>> test = db.test
>>> test.find_one();
{u'_id': ObjectId('505bc6c0848e6c0a30d789ee'), u'id': 1, u'name': u'wangyf'}
>>>
2. 插入一行数据:
>>> test.insert({'id':2,'name':'w2'})
ObjectId('505d3fb207a15601a9000000')
3. 查询多行:
for str in test.find():
str
{u'_id': ObjectId('505bc6c0848e6c0a30d789ee'), u'id': 1, u'name': u'wangyf'}
{u'_id': ObjectId('505d3fb207a15601a9000000'), u'id': 2, u'name': u'w2'}
4. 查询用户名为w2的记录
>>> for str in test.find({'name':'w2'}):
... str
...
{u'_id': ObjectId('505d3fb207a15601a9000000'), u'id': 2, u'name': u'w2'}
>>>
5. 记录总数
>>> test.count()
2
>>>