• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中的MongoDB基本操作:连接、查询实例

Python中的MongoDB基本操作:连接、查询实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python mongodb,python连接mongodb,python操作mongodb,python3 mongodb,python查询mongodb等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可护展的高性能数据存储解决方案。它的特点是高性能、易部署、易使用,存储数据非常方便。

MongoDB 简单使用

联接数据库

In [1]: import pymongo
In [2]: from pymongo import Connection
In [3]: connection = Connection('192.168.1.3', 27017) //创建联接
</div>

Connection 相关参数

Connection([host='localhost'[, port=27017[, pool_size=None[, auto_start_request=None[, timeout=None[, slave_okay=False[, network_timeout=None[, document_class=dict[, tz_aware=True]]]]]]]]])
</div>

数据库操作

In [9]: c.database_names() //列出所有数据库名称
Out[9]: [u'test', u'admin', u'yuhen', u'sms', u'local']

In [10]: c.server_info() //查看服务器相关信息
Out[10]:
{u'bits': 64,
 u'gitVersion': u'nogitversion',
 u'ok': 1.0,
 u'sysInfo': u'Linux yellow 2.6.24-27-server #1 SMP Fri Mar 12 01:23:09 UTC 2010 x86_64 BOOST_LIB_VERSION=1_40',
 u'version': u'1.2.2'}

In [16]: db = c['test'] //选择数据库
In [17]: db.collection_names() //列出当前数据库中所有集合名称
Out[17]: [u'system.indexes', u'fs.files', u'fs.chunks', u'test_gao']

In [23]: db.connection //查看联接信息
Out[23]: Connection('192.168.1.3', 27017)

In [24]: db.create_collection('test_abeen') //创建新集合
Out[24]: Collection(Database(Connection('192.168.1.3', 27017), u'test'), u'test_abeen')

In [25]: db.last_status() //查看上次操作状态
Out[25]: {u'err': None, u'n': 0, u'ok': 1.0}

In [26]: db.name //查看当前数据库名称
Out[26]: u'test'

In [27]: db.profiling_info() //查看配置信息
Out[27]: []

In [28]: db.profiling_level()
Out[28]: 0.0
</div>

集合操作
In [31]: db.collection_names() //查看当前数据库所有集合名称
Out[31]:
[u'system.indexes',
 u'fs.files',
 u'fs.chunks',
 u'test_gao',
 u'system.users',
 u'test_abeen']

In [32]: c = db.test_abeen //选择集合
In [33]: c.name //查看当前集合名称
Out[33]: u'test_abeen'

In [35]: c.full_name //查看当前集合全名
Out[35]: u'test.test_abeen'
In [36]: c.database //查看当前集合数据库相关信息
Out[36]: Database(Connection('192.168.1.3', 27017), u'test')

In [38]: post = {"author":"Mike","text":"this is a test by abeen"}
In [39]: posts = db.posts
In [40]: posts.insert(post) //向数据库集合插入文档,默认创建集合
Out[40]: ObjectId('4c358492421aa91e70000000')
In [41]: db.collection_names() //显示所有集合名称
Out[41]:
[u'system.indexes',
 u'fs.files',
 u'fs.chunks',
 u'test_gao',
 u'system.users',
 u'test_abeen',
 u'posts']

In [42]: posts.find_one() //从集合查找信息
Out[42]:
{u'_id': ObjectId('4c358492421aa91e70000000'),
 u'author': u'Mike',
 u'text': u'this is a test by abeen'}
In [52]: p.update({"author":"Mike"},{"$set":{"author":"abeen","text":"this is a test by abeen shan shan"}})//更新集合文档信息
In [55]: list(p.find())
Out[55]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
  u'author': u'abeen',
  u'text': u'this is a test by abeen shan shan'}]

In [96]: list(posts.find())
Out[96]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
  u'author': u'Mike',
  u'text': u'this is a test by abeen'},
 {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
 {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'},
 {u'_id': ObjectId('4c358abb421aa91e70000001'),
  u'a': u'abeen',
  u'b': u'this bb is updated'}]
In [97]: posts.remove({"a":"abeen"}) //删除符合条件的文档
In [98]: list(posts.find())
Out[98]:
[{u'_id': ObjectId('4c358492421aa91e70000000'),
  u'author': u'Mike',
  u'text': u'this is a test by abeen'},
 {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
 {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}]

In [102]: db.collection_names()
Out[102]:
[u'system.indexes',
 u'fs.files',
 u'fs.chunks',
 u'test_gao',
 u'system.users',
 u'test_abeen',
 u'posts',
 u'doc_abeen']

In [104]: db.drop_collection("doc_abeen") //删除集合
In [105]: db.collection_names()
Out[105]:
[u'system.indexes',
 u'fs.files',
 u'fs.chunks',
 u'test_gao',
 u'system.users',
 u'test_abeen',
 u'posts']
</div>

代码
In [113]: result = db.posts.find({"a":"aa"})//查找
In [114]: type(result)
Out[114]: <class 'pymongo.cursor.Cursor'>
In [119]: list(result)
Out[119]:
[{u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'},
 {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a': u'aa', u'b': u'bb'}]
</div>

find格式
find([spec=None[, fields=None[, skip=0[, limit=0[, timeout=True[, snapshot=False[, tailable=False[, sort=None[, max_scan=None[, as_class=None[, **kwargs]]]]]]]]]]])
</div>

代码
In [120]: db.posts.count()//当前集合文档数
Out[120]: 3
In [121]: type(db.posts)
Out[121]: <class 'pymongo.collection.Collection'>

In [138]: posts.rename('test_abeen')//重命名当前集合
In [139]: db.collection_names()
Out[139]:
[u'system.indexes',
 u'fs.files',
 u'fs.chunks',
 u'test_gao',
 u'system.users',
 u'test_abeen']

In [151]: for post in c.find({"a":"aa"}).sort("a"): //查询并排序列
    post
Out[152]: {u'_id': ObjectId('4c358ad4421aa91e70000002'), u'a': u'aa', u'b': u'bb'}
Out[152]: {u'_id': ObjectId('4c358ad9421aa91e70000003'), u'a

您可能想查找下面的文章:

  • 在 Python 应用中使用 MongoDB的方法
  • 在 Python 应用中使用 MongoDB的方法
  • Python+MongoDB自增键值的简单实现
  • Python中MySQL数据迁移到MongoDB脚本的方法
  • Python中MySQL数据迁移到MongoDB脚本的方法
  • Python保存MongoDB上的文件到本地的方法
  • Python简单连接MongoDB数据库的方法
  • Python简单连接MongoDB数据库的方法
  • python&MongoDB爬取图书馆借阅记录
  • python操作mongodb根据_id查询数据的实现方法

相关文章

  • Python2.x中文乱码问题解决方法
  • Django中的“惰性翻译”方法的相关使用
  • python文件和目录操作方法大全(含实例)
  • python中的__init__ 、__new__、__call__小结
  • Python的MongoDB模块PyMongo操作方法集锦
  • Python实现的生成自我描述脚本分享(很有意思的程序)
  • python 解析html之BeautifulSoup
  • python实现用户登录系统
  • 在Mac OS上搭建Python的开发环境
  • Python中的Matplotlib模块入门教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 初学Python实用技巧两则
    • 使用Python的Scrapy框架编写web爬虫的简单示例
    • Python中使用platform模块获取系统信息的用法教程
    • 深入理解Python中变量赋值的问题
    • Python计算字符宽度的方法
    • Python os模块中的isfile()和isdir()函数均返回false问题解决方法
    • python为tornado添加recaptcha验证码功能
    • 在Python的while循环中使用else以及循环嵌套的用法
    • itchat和matplotlib的结合使用爬取微信信息的实例
    • 简单谈谈python中的Queue与多进程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有