欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

python利用pymongo模块操作mongodb

程序员文章站 2024-01-03 11:48:40
...

前段时间,公司的测试环境中的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 发表

上一篇:

下一篇: