learn of python driver for mongodb

一、安装:

  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
>>>


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值