在centos6.*下升级python2.6.6到python2.7.2 然后安装pymongo
####
在centos6下python版本是2.6.6但是由于生产环境python 需要2.7
下载python2.7.2.tar.gz
解压后安装,一定要先安装openssl-devel
./configure --prefix=/opt/python27
mv /usr/bin/python /usr/bin/python2.6
ln -s /opt/python27/bin/python /usr/bin/python
下载setuptools-3.4.1.zip
python的官网太慢,我们一定从国内的镜像网下
pypi.douban.com
解压setuptools
然后直接
python setup.py install
安装完后再到
cd /opt/python27/bin
./easy_install -i pymongo
安装完成!
创建Connection时,指定host及port参数
>>> import pymongo
>>> conn = pymongo.Connection(host='127.0.0.1',port=27017)
连接数据库
>>> db = conn.DataBase
或
>>> db = conn['DataBase']
登陆
>>>db.authenticate('username', 'password')
删除表
>>>db.drop_collection('Collection')
退出
>>>db.logout()
连接聚集
>>> Collection = db.Collection
或
>>> Collection = db["Collection"]
查看全部聚集名称
>>> db.collection_names()
查看聚集的一条记录
>>> db.Collection.find_one()
>>> db.Collection.find_one({"UserName":"keyword"})
查看聚集的字段
>>> db.Collection.find_one({},{"UserName":1,"Email":1})
{u'UserName': u'libing', u'_id': ObjectId('4ded95c3b7780a774a099b7c'), u'Email': u'libing@35.cn'}
>>> db.Collection.find_one({},{"UserName":1,"Email":1,"_id":0})
{u'UserName': u'libing', u'Email': u'libing@35.cn'}
查看聚集的多条记录
>>> for item in db.Collection.find():
item
>>> for item in db.Collection.find({"UserName":"libing"}):
item["UserName"]
查看聚集的记录统计
>>> db.Collection.find().count()
>>> db.Collection.find({"UserName":"keyword"}).count()
聚集查询结果排序
>>> db.Collection.find().sort("UserName") --默认为升序
>>> db.Collection.find().sort("UserName",pymongo.ASCENDING) --升序
>>> db.Collection.find().sort("UserName",pymongo.DESCENDING) --降序
聚集查询结果多列排序
>>> db.Collection.find().sort([("UserName",pymongo.ASCENDING),("Email",pymongo.DESCENDING)])
db.Collection.find({}).sort({'age': 1}) # 排序
db.Collection.find({}).skip(2).limit(5) # 切片
添加记录
>>> db.Collection.insert({"CollectionID":21,"UserName":"libing"})
修改记录
>>> db.Collection.update({"UserName":"libing"},{"$set":{"Email":"libing@126.com","Password":"123"}})
删除记录
>>> db.Collection.remove() -- 全部删除
>>> db.Collection.remove({"UserName":"keyword"})
对应mysql
>>>db.Collection.find({'name':'foobar'}) <==> select * from Collection where name='foobar'
>>>db.Collection.find() <==> select * from Collection
>>>db.Collection.find({'ID':10}).count() <==> select count(*) from Collection where ID=10
>>>db.Collection.find().skip(10).limit(20) <==> select * from Collection limit 10,20
>>>db.Collection.find({'ID':{$in:[25,35,45]}}) <==> select * from Collection where ID in (25,35,45)
>>>db.Collection.find().sort({'ID':-1}) <==> select * from Collection order by ID desc
>>>db.Collection.distinct('name',{'ID':{$lt:20}}) <==> select distinct(name) from Collection where ID<20
>>>db.Collection.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from Collection group by name
>>>db.Collection.find('this.ID<20',{name:1}) <==> select name from Collection where ID<20
>>>db.Collection.insert({'name':'foobar','age':25})<==>insert into Collection ('name','age') values('foobar',25)
>>>db.Collection.remove({}) <==> delete * from Collection
>>>db.Collection.remove({'age':20}) <==> delete Collection where age=20
>>>db.Collection.remove({'age':{$lt:20}}) <==> elete Collection where age<20
>>>db.Collection.remove({'age':{$lte:20}}) <==> delete Collection where age<=20
>>>db.Collection.remove({'age':{$gt:20}}) <==> delete Collection where age>20
>>>db.Collection.remove({'age':{$gte:20}}) <==> delete Collection where age>=20
>>>db.Collection.remove({'age':{$ne:20}}) <==> delete Collection where age!=20
>>>db.Collection.update({'name':'foobar'},{$set:{'age':36}}) <==> update Collection set age=36 where name='foobar'
>>>db.Collection.update({'name':'foobar'},{$inc:{'age':3}}) <==> update Collection set age=age+3 where name='foobar
阅读(1583) | 评论(0) | 转发(0) |