您好,欢迎访问一九零五行业门户网

python利用pymongo模块操作mongodb

前段时间,公司的测试环境中的mongo数据有一部分要导入到线上的环境。 开发给提供了一堆的objectid,而且要求导入到线上之后,这个objectid还不能变。 于是我就想用python来查询并且导入到线上。顺便也学习下用python操作mongodb, 结果遇到一个坑。 这段时
前段时间,公司的测试环境中的mongo数据有一部分要导入到线上的环境。
开发给提供了一堆的objectid,而且要求导入到线上之后,这个objectid还不能变。
于是我就想用python来查询并且导入到线上。顺便也学习下用python操作mongodb,
结果遇到一个坑。
这段时间闲一些,于是就整理出来分享给大家。
一、首先是安装python的pymongo模块:
三种安装方式pip/easy_install/源码
#//pip pip install pymongo #//easy_install easy_install pymongo #//源码 wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.7.tar.gz tar zxvf pymongo-2.7.tar.gz cd pymongo-2.7 python setup.py install
原文地址:http://www.linuxyan.com/shell/320.html
二、使用:
#!/usr/bin/python import pymongo import time conn = pymongo.connection(127.0.0.1,27017) db = conn.test #连接库test db.authenticate(tage,123) #用户认证 #//插入数据,_id自动创建 post = {id: 1, author: mike, text: my first blog post!, tags: [mongodb, python, pymongo], date: time.strftime('%y-%m-%d %h:%m:%s')} posts = db.posts posts.insert(post) #把post数据插入posts聚合(表)中,返回一个objectid('...') #//批量插入(一个列表里面包含了2个字典),_id自动创建 new_posts = [{id: 2, author: mike, text: another post!, tags: [bulk, insert], date: time.strftime('%y-%m-%d %h:%m:%s')}, {id: 3, author: eliot, title: mongodb is fun, text: and pretty easy too!, date: time.strftime('%y-%m-%d %h:%m:%s')}] posts = db.posts posts.insert(new_posts) #把new_posts数据插入posts聚合(表)中,返回2个objectid('...') #//删除数据 db.posts.remove() #删除posts聚合(表)中所有数据 db.posts.remove({'id':1}) #删除posts聚合(表)中id为1的数据 #//更新数据 db.posts.update({'id':1},{$set:{text:cscscascs}}) #更新一个value db.posts.update({'id':1},{$set:{text:cscscascs,title:test title}}) #更新多个value #//查询数据 db.collection_names() #查询所有聚合名称 db.posts.count() #统计posts聚合中的数据数量 db.posts.find() #查询posts中所有内容 db.posts.find_one({author:mike}) #根据条件查询posts中数据 db.posts.find({author:mike}).sort('author') #--默认为升序 db.posts.find({author:mike}).sort('author',pymongo.ascending) #升序 db.posts.find({author:mike}).sort('author',pymongo.descending) #降序
原文地址:http://www.linuxyan.com/shell/320.html
三、遇到的坑
刚才插入数据成功的时候,会返回一个objectid(‘…’)
于是当我用{‘_id’:”objectid(‘…’)”}查询的时候缺什么都没查到
如下:
>>> import pymongo>>> import time>>> db = pymongo.connection(192.168.xx.xx,27017).linuxyan>>> posts = db.posts>>> post = {id: 1,... author: mike,... text: my first blog post!,... tags: [mongodb, python, pymongo],... date: time.strftime('%y-%m-%d %h:%m:%s')}>>> posts.insert(post)objectid('53bd5a5fe138235f74b67563')#//插入数据成功#//利用{'author':'mike'} 测试查询正常>>> posts.find_one({'author':'mike'}){u'_id': objectid('53bd5a5fe138235f74b67563'), u'author': u'mike',....}#//利用{'_id':objectid('53bd5a5fe138235f74b67563')}查询为空>>>posts.find_one({'_id':objectid('53bd5a5fe138235f74b67563')}) #//如何利用objectid来查询?>>> from bson import objectid>>> posts.find_one({'_id':objectid('53bd5a5fe138235f74b67563')}) { u'_id': objectid('53bd5a5fe138235f74b67563'), u'author': u'mike',....}#//原来objectid是一个对象,而不是一个字符串,此时我只能呵呵,折腾两个多小时。
本文固定链接: http://www.linuxyan.com/shell/320.html转载请注明: admin 2014年07月09日 于 ㄨ销声匿迹、linux 发表
其它类似信息

推荐信息