pymongo 使用测试
>>> import pymongo
>>> uri = "mongodb://recall:123456@oceanic.mongohq.com:10062/must"
>>> client = pymongo.MongoClient(uri) #连接到数据库
>>> db = client.must #选择数据库名
>>> db.collection_names #查看所有聚集名,相当与show_tables
<bound method Database.collection_names of Database(MongoClient('oceanic.mongohq.com', 10062), u'must')>
>>> table = db.mytable #创建聚集
>>> count = table.count() #查看聚集中的数目
>>> count
0
>>> monster = {"name":"Dracule","occupation":"Blood Suker","tags":["vampire","teeth","bat"],"data":""}
>>> insert_id = table.insert(monster) #插入数据
>>> for monster_one in table.find():
... print monster_one
...
...
{u'occupation': u'Blood Suker', u'_id': ObjectId('53510fcad6ca3fb153c5681d'), u'data': u'', u'name': u'Dracule', u'tags': [u'vampire', u'teeth', u'bat']}
>>> print table.find_one({"name":"Dracule"})
{u'occupation': u'Blood Suker', u'_id': ObjectId('53510fcad6ca3fb153c5681d'), u'data': u'', u'name': u'Dracule', u'tags': [u'vampire', u'teeth', u'bat']}
可查看 http://docs.mongohq.com/languages/python.html
import os
import datetime
import pymongo
from pymongo import MongoClient # Grab our connection information from the MONGOHQ_URL environment variable
# (mongodb://linus.mongohq.com:10045 -u username -pmy_password)
MONGO_URL = os.environ.get('MONGOHQ_URL')
#connection = Connection(MONGO_URL)
client = MongoClient(MONGO_URL) # Specify the database
db = client.mytestdatabase
# Print a list of collections
print db.collection_names() # Specify the collection, in this case 'monsters'
collection = db.monsters # Get a count of the documents in this collection
count = collection.count()
print "The number of documents you have in this collection is:", count # Create a document for a monster
monster = {"name": "Dracula",
"occupation": "Blood Sucker",
"tags": ["vampire", "teeth", "bat"],
"date": datetime.datetime.utcnow()
} # Insert the monster document into the monsters collection
monster_id = collection.insert(monster) # Print out our monster documents
for monster in collection.find():
print monster # Query for a particular monster
print collection.find_one({"name": "Dracula"})
pymongo 使用测试的更多相关文章
- python使用mongodb
系统环境:Windows 10 ,python3.x 1.安装mongodb mongodb下载官网:https://www.mongodb.com/download-center?jmp=nav#c ...
- python3连接Mongodb
前提条件,安装过Mongondb,并且装一下Robomongo(为了更加直观地看到测试时数据的变化 ) 1.安装PyMySQL pip install pymongo 2.测试 import pymo ...
- 2.0 (2)测试pymongo
在数据库中创建数据库.表,插入数据. from pymongo import MongoClient host = "localhost" port = 27017 client ...
- Windows平台下为Python添加MongoDB支持PyMongo
到Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe 安装pymongo-2.6.3.win-amd64-py2.7.exe 参照官方的用例进行测试 打开命令提示符 ...
- CentOS平台下为Python添加MongoDB支持PyMongo
下载PyMongo [root@leezhen ~]# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.6.3.tar ...
- 从零使用Python测试。Testing Using Python.
0. 写在前面 本人使用Python测试已有多年,略有些皮毛经验.每次有新员工入职,都会从头教一遍如何入门上手使用Python进行测试.趁这段有空,整理成文档,也好方便后续新员工学习.文章如有不妥之处 ...
- pymongo 学习总结
1.简介 MongoDB是一种强大.灵活.追求性能.易扩展的数据存储方式.是面向文档的数据库,不是关系型数据库,是NoSQL(not only SQL)的一种.所谓的面向文档,就是将原来关系型数据库中 ...
- pymongo连接MongoDB
导语 pymongo 是目前用的相对普遍一个python用来连接MongoDB的库,是工作中各种基本需求都能满足具体api可以参考 pymongo APIpymongo github 安装 Mongo ...
- python爬虫之MongoDB测试环境安装
一. 下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二. 安装 1>设置mongoDB ...
随机推荐
- openstack rpc机制
一.概述: 在openstack项目中,api的调用规则: 跨项目:如nova调用keystone, glance,cinder等,使用rest api(通过相应的python-XXXclient 库 ...
- 如何禁用Visual Studio 2013的浏览器链接功能
VS2013新增的Browser Link功能虽然“强大”,但我并不需要. 但默认是开启的,会在页面中自动添加如下的代码,真是烦人! <!-- Visual Studio Browser Lin ...
- Android开发 Fragment中调用startActivityForResult返回错误的requestCode
返回错误的requestCode返回值为65537,在Fragment里调用startActivityForResult,就必须在Fragment里处理onActivityResult.
- 一段js代码
原文地址 [].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math ...
- 坑爹的高德地图API
症状 ld: '-[MASearch poiSearchWithOption:]' in *****/Release-iphonesimulator/libMASearchKit.a(MASearch ...
- jlink下载不进去程序
- [转]SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- Vue 点击波浪特效指令
- Knockout: 实践CSS绑定和afterkeydown事件, 给未通过校验的输入框添加红色边框突出显示; 使用afterkeydown事件自动将输入转大写字母.
目的: 实践一下Knockout提供的CSS绑定功能和afterkeydown事件. 步骤: 先在htm中添加.error的css样式, 并在输入框中绑定css: { error: !firstNam ...
- Oracle多表关联如何更新多个字段
注意点:1.被update主表一定要加上过滤条件.2.查询出来更新结果集,同时也要作为被更新主表的条件,作为同步大家都是更新这部分数据.update student stu set (stu.name ...