>>> 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 使用测试的更多相关文章

  1. python使用mongodb

    系统环境:Windows 10 ,python3.x 1.安装mongodb mongodb下载官网:https://www.mongodb.com/download-center?jmp=nav#c ...

  2. python3连接Mongodb

    前提条件,安装过Mongondb,并且装一下Robomongo(为了更加直观地看到测试时数据的变化 ) 1.安装PyMySQL pip install pymongo 2.测试 import pymo ...

  3. 2.0 (2)测试pymongo

    在数据库中创建数据库.表,插入数据. from pymongo import MongoClient host = "localhost" port = 27017 client ...

  4. Windows平台下为Python添加MongoDB支持PyMongo

    到Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe 安装pymongo-2.6.3.win-amd64-py2.7.exe 参照官方的用例进行测试 打开命令提示符 ...

  5. CentOS平台下为Python添加MongoDB支持PyMongo

    下载PyMongo [root@leezhen ~]# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.6.3.tar ...

  6. 从零使用Python测试。Testing Using Python.

    0. 写在前面 本人使用Python测试已有多年,略有些皮毛经验.每次有新员工入职,都会从头教一遍如何入门上手使用Python进行测试.趁这段有空,整理成文档,也好方便后续新员工学习.文章如有不妥之处 ...

  7. pymongo 学习总结

    1.简介 MongoDB是一种强大.灵活.追求性能.易扩展的数据存储方式.是面向文档的数据库,不是关系型数据库,是NoSQL(not only SQL)的一种.所谓的面向文档,就是将原来关系型数据库中 ...

  8. pymongo连接MongoDB

    导语 pymongo 是目前用的相对普遍一个python用来连接MongoDB的库,是工作中各种基本需求都能满足具体api可以参考 pymongo APIpymongo github 安装 Mongo ...

  9. python爬虫之MongoDB测试环境安装

    一.   下载 从http://www.mongodb.org/downloads地址中下载:mongodb-linux-x86_64-2.4.11.tar 二.  安装 1>设置mongoDB ...

随机推荐

  1. C语言学习笔记 (006) - 二维数组传参的三种表现形式

    # include <stdio.h> # include <stdlib.h> # define M # define N int getdate(int (*sp)[M]) ...

  2. POJ 2230 Watchcow (欧拉回路)

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5258   Accepted: 2206   Specia ...

  3. hibernate的findByExample 外键参数查询解决方案

    用了这么长时间的hibernate/spring,如果不是今天用的findByExample方法到现在还不知道findByExample的机制.惭愧 Class User{String usernam ...

  4. Swift 基本运算符

    前言 Swift 语言支持大部分标准 C 语言的运算符,并且改进了许多特性来使我们的代码更加规范,其中主要包含算数运算符.区间运算符.逻辑运算符.关系运算符.赋值运算符.自增自减运算符.溢出运算符等. ...

  5. DSYMTools App Bug 分析工具

    1.DSYMTools 简介 DSYMTools 是一个可以解析 iOS Crash 文件的工具. GitHub 下载地址 DSYMTools 其它下载地址 DSYMTools v1.0.5 for ...

  6. MySQL (1366, "Incorrect string value: '\\xF0\\x9F\\x8E\\xAC\\xE5\\x89...' for column 'description' at row 1")

    (1366, "Incorrect string value: '\\xF0\\x9F\\x8E\\xAC\\xE5\\x89...' for column 'description' at ...

  7. 使用 properties 配置文件装配 bean 的方式

    在spring中将bean 注册到spring 容器中常见的有三种方式(两类): 先说明配置文件内容:application.yml,有一段配置如下 persons: youtube: name: y ...

  8. jQuery的dialog弹窗实现

    jQuery实现dialog弹窗: html代码如下: <input type="button" onclick="performances();" va ...

  9. 基础003_V7-Memory Resources

    一.综述 参考ug473.pdf. 常用Memory 资源: 在IP核中,Block memory(distributed memory为CLB中的资源): 通常选用Native,而不用AXI接口: ...

  10. centos 7 下图形验证码乱码

    工作中遇到一个问题:同样的代码在centos 6.5下图形验证码是正常的 但是在centos 7下面是乱码 centos 6.5 的系统字体库目录 [wwwad@P2P-test2 fonts]$ p ...