Python 操作mongodb数据库

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介:   查看数据库  from pymongo import MongoClient  connect=MongoClient(host='localhost', port=27017, username="root", password="123456")  connect=MongoClient('mongodb://localhost:27017/', username="root", password="123456")  print(connect.list_database_names())

  查看数据库

  from pymongo import MongoClient

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456")

  connect=MongoClient('mongodb://localhost:27017/', username="root", password="123456")

  print(connect.list_database_names())

  获取二手游戏账号转让平台数据库实例

  test_db=connect['test']

  获取collection实例

  collection=test_db['students']

  插入一行document, 查询一行document,取出一行document的值

  from pymongo import MongoClient

  from datetime import datetime

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  # 构建document

  document={"author": "Mike", "text": "My first blog post!", "tags": ["mongodb", "python", "pymongo"], "date": datetime.now()}

  # 插入document

  one_insert=collection.insert_one(document=document)

  print(one_insert.inserted_id)

  # 通过条件过滤出一条document

  one_result=collection.find_one({"author": "Mike"})

  # 解析document字段

  print(one_result, type(one_result))

  print(one_result['_id'])

  print(one_result['author'])

  注意:如果需要通过id查询一行document,需要将id包装为ObjectId类的实例对象

  from bson.objectid import ObjectId

  collection.find_one({'_id': ObjectId('5c2b18dedea5818bbd73b94c')})

  插入多行documents, 查询多行document, 查看collections有多少行document

  from pymongo import MongoClient

  from datetime import datetime

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  documents=[{"author": "Mike","text": "Another post!","tags": ["bulk", "insert"], "date": datetime(2009, 11, 12, 11, 14)},

  {"author": "Eliot", "title": "MongoDB is fun", "text": "and pretty easy too!", "date": datetime(2009, 11, 10, 10, 45)}]

  collection.insert_many(documents=documents)

  # 通过条件过滤出多条document

  documents=collection.find({"author": "Mike"})

  # 解析document字段

  print(documents, type(documents))

  print(''300)

  for document in documents:

  print(document)

  print(''300)

  result=collection.count_documents({'author': 'Mike'})

  print(result)

  范围比较查询

  from pymongo import MongoClient

  from datetime import datetime

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  # 通过条件过滤时间小于datetime(2021, 1,1,15,40,3) 的document

  documents=collection.find({"date": {"$lt": datetime(2021, 1,1,15,40,3)}}).sort('date')

  # 解析document字段

  print(documents, type(documents))

  print(''300)

  for document in documents:

  print(document)

  创建索引

  from pymongo import MongoClient

  import pymongo

  from datetime import datetime

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  # 创建字段索引

  collection.create_index(keys=[("name", pymongo.DESCENDING)], unique=True)

  # 查询索引

  result=sorted(list(collection.index_information()))

  print(result)

  document修改

  from pymongo import MongoClient

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  result=collection.update({'name': 'robby'}, {'$set': {"name": "Petter"}})

  print(result)

  注意:还有update_many()方法

  document删除

  from pymongo import MongoClient

  connect=MongoClient(host='localhost', port=27017, username="root", password="123456",)

  # 获取db

  test_db=connect['test']

  # 获取collection

  collection=test_db['students']

  result=collection.delete_one({'name': 'Petter'})

  print(result.deleted_count)

  注意:还有delete_many()方法

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
5天前
|
存储 NoSQL Shell
MongoDB 创建数据库
10月更文挑战第12天
26 4
|
4天前
|
存储 NoSQL MongoDB
基于阿里云数据库MongoDB版,微财数科“又快又稳”服务超7000万客户
选择MongoDB主要基于其灵活的数据模型、高性能、高可用性、可扩展性、安全性和强大的分析能力。
|
14天前
|
存储 关系型数据库 数据库
轻量级数据库的利器:Python 及其内置 SQLite 简介
轻量级数据库的利器:Python 及其内置 SQLite 简介
22 3
|
16天前
|
数据库连接 Linux 数据库
GBase 8s数据库连接 – Python
GBase 8s数据库连接 – Python
|
3天前
|
存储 NoSQL MongoDB
mongodb的数据库表怎么创建
在此过程中,理解并掌握这些基本操作,是深入探索MongoDB魅力,乃至构建高效数据解决方案的关键所在。通过实践,您将更加深刻地体会到这种随需应变的数据管理模式带来的便利与效率提升。
8 0
|
4天前
|
存储 NoSQL MongoDB
小川科技携手阿里云数据库MongoDB:数据赋能企业构建年轻娱乐生态
基于MongoDB灵活模式的特性,小川实现了功能的快速迭代和上线,而数据库侧无需任何更改
|
4天前
|
运维 NoSQL BI
简道云搭载阿里云MongoDB数据库,帮助数以万计企业重构业务系统
通过与MongoDB和阿里云团队的合作,让简道云少走了弯路,保障了线上服务的长期稳定运行,提高了吞吐效率,并相应降低了线上运行成本
|
5天前
|
SQL NoSQL MongoDB
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
20 0
|
4月前
|
存储 NoSQL Redis
Python—操作redis的一些心得
Python—操作redis的一些心得
24 0
|
存储 NoSQL 数据库