本文是一个Python 使用MongoDB的简单教程,将使用pymongo对MongoDB进行的各种操作进行了简单的汇总,NoSQLFan进行了简单整理,使用Python的同学可以看一看。
下载相应平台的版本,解压即可。为方便使用,将bin路径添加到系统path环境变量里。其中mongod是服务器,mongo是客户shell,然后创建数据文件目录:在c盘下创建data文件夹,里面创建db文件夹。
基本使用:
安装对应语言的Driver,Python 安装 pymongo
使用方法总结,摘自官方教程
创建连接
>>> import pymongo >>> connection=pymongo.Connection('localhost',27017) |
切换数据库
>>> db = connection.test_database |
获取collection
>>> collection = db.test_collection |
db和collection都是延时创建的,在添加Document时才真正创建
文档添加,_id自动创建
>>> import datetime >>> post = {"author": "Mike", ... "text": "My first blog post!", ... "tags": ["mongodb", "python", "pymongo"], ... "date": datetime.datetime.utcnow()} >>> posts = db.posts >>> posts.insert(post) ObjectId('...') |
批量插入
>>> new_posts = [{"author": "Mike", ... "text": "Another post!", ... "tags": ["bulk", "insert"], ... "date": datetime.datetime(2009, 11, 12, 11, 14)}, ... {"author": "Eliot", ... "title": "MongoDB is fun", ... "text": "and pretty easy too!", ... "date": datetime.datetime(2009, 11, 10, 10, 45)}] >>> posts.insert(new_posts) [ObjectId('...'), ObjectId('...')] |
获取所有collection(相当于SQL的show tables)
>>> db.collection_names() [u'posts', u'system.indexes'] |
获取单个文档
>>> posts.find_one() {u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']} |
查询多个文档
>> for post in posts.find(): ... post ... {u'date': datetime.datetime(...), u'text': u'My first blog post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'mongodb', u'python', u'pymongo']} {u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'bulk', u'insert']} {u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('...'), u'author': u'Eliot', u'title': u'MongoDB is fun'} |
加条件的查询
>>> posts.find_one({"author": "Mike"}) |
高级查询
>>> posts.find({"date": {"$lt": d}}).sort("author") |
统计数量
加索引
>>> from pymongo import ASCENDING, DESCENDING >>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)]) u'date_-1_author_1' |
查看查询语句的性能
>>> posts.find({"date": {"$lt": d}}).sort("author").explain()["cursor"] u'BtreeCursor date_-1_author_1' >>> posts.find({"date": {"$lt": d}}).sort("author").explain()["nscanned"] 2 |
附自己总结的一点小心得,仅供参考
缺点
- 不是全盘取代传统数据库(NoSQLFan:是否能取代需要看应用场景)
- 不支持复杂事务(NoSQLFan:MongoDB只支持对单个文档的原子操作)
- 文档中的整个树,不易搜索,4MB限制?(NoSQLFan:1.8版本已经修改为16M)
特点(NoSQLFan:作者在这里列举的很多只是一些表层的特点):
- 文档型数据库,表结构可以内嵌
- 没有模式,避免空字段开销(Schema Free)
- 分布式支持
- 查询支持正则
- 动态扩展架构
- 32位的版本最多只能存储2.5GB的数据(NoSQLFan:最大文件尺寸为2G,生产环境推荐64位)
名词对应
- 一个数据项叫做 Document(NoSQLFan:对应MySQL中的单条记录)
- 一个文档嵌入另一个文档(comment 嵌入 post)叫做 Embed
- 储存一系列文档的地方叫做 Collections(NoSQLFan:对应MySQL中的表)
- 表间关联,叫做 Reference
分享到:
相关推荐
python使用mongoDB
通过python语言mongodb数据库惊醒操作,包括连接、上传、下载等操作
Python MongoDB 应用开发,构建高效稳定数据库应用系统
【Python高级开发课程 高级教程】课程列表 01 Python语言开发要点详解....07 Python操作MongoDB数据库.pptx 08 Python多线程 多进程开发.pptx 09 Python爬虫框架Scrapy实战.pptx 10 Python Web开发框架Django实战.pptx
Python + MongoDB开发的百度云爬虫.zip Python + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB...
通过python启动mongodb,动态灵活设置mongodb.conf文件,并且实现储存数据和log在程序文件夹下,
本教材的内容结合了开发中的经验,并参考了《Python数据库编程》《Python和MongoDB开发指南》《Python数据库编程指南》等书,力求给读者一个清晰简单的示范案例,主要包括MongoDB和传统型数据库对比,MongoDB的常规...
亲测可用 基于Python + MongoDB 开发的百度云爬虫 基于Python + MongoDB 开发的百度云爬虫 基于Python + MongoDB 开发的百度云爬虫 基于Python + MongoDB 开发的百度云爬虫
基于Python+MongoDB开发的百度云资源爬虫.zip 基于Python+MongoDB开发的百度云资源爬虫.zip 基于Python+MongoDB开发的百度云资源爬虫.zip 基于Python+MongoDB开发的百度云资源爬虫.zip
iHealth 项目的内容爬虫(一个基于 python 和 MongoDB 的医疗咨询爬虫iHealth 项目的内容爬虫(一个基于 python 和 MongoDB 的医疗咨询爬虫iHealth 项目的内容爬虫(一个基于 python 和 MongoDB 的医疗咨询爬虫...
python、mongodb、redis.docx
python实现连接mongodb并执行相关操作,添加数据,并查询数据
2018年最新的数据挖掘资料,介绍Python和MongoDB的使用,全高清英文版,Apress出版社
MongoDB上的文档通过GridFS来操作,Python也可以通过pymongo连接MongoDB数据库,使用pymongo模块的gridfs方法操作文档。以下示例是把MongoDB上GridFS存的excel文档保存到本地。 from pymongo import MongoClient ...
本文实例讲述了Python操作mongodb数据库进行模糊查询操作。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import pymongo import re from pymongo import MongoClient #创建连接 #10.20.66.106 client...
在项目中实现的python3实现的mongodb的查询类库
[奥莱理] 基于 MongoDB 的 Python 应用开发 (英文版) [奥莱理] MongoDB and Python Patterns and processes for the popular document-oriented database (E-Book) ☆ 出版信息:☆ [作者信息] Niall O'Higgins ...
python操作mongodb的密码强度验证.pdf