登录  /  注册
python利用pymongo模块操作mongodb
php中文网
发布: 2016-06-07 16:37:00
原创
546人浏览过

前段时间,公司的测试环境中的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 于 ㄨ销声匿迹、Linux 发表

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学