Python操作MongoDB

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: Python操作MongoDB
mongod --auth --dbpath="/usr/local/mongodb/data" --logpath="/usr/local/mongodb/logs/mongod.log" --install
    #创建admin数据表
    use admin
    #创建管理用户的用户
    db.createUser(
      {
        user: "root",
        pwd: "123456",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )
    #认证
    db.auth('user','pass')
    #切换数据表
    use testdb
    #创建当前数据表的操作用户
    db.createUser(
      {
        user: "test",
        pwd: "123456",
        roles: [ { role: "readWrite", db: "testdb" } ]
      }
    )
复制代码


class MONGODB_CONFIG:
  HOST = "127.0.0.1"
  PORT = 27017
  DB_NAME = "testdb"
  USER_NAME = "test"
  PASSWORD = "123456"
import config
import pymongo
# 创建连接对象 ''' mongodb://localhost:27017/ '''
client = pymongo.MongoClient(host=config.MONGODB_CONFIG.HOST, port=config.MONGODB_CONFIG.PORT)
# 连接数据库
db = client[config.MONGODB_CONFIG.DB_NAME]
db.authenticate(config.MONGODB_CONFIG.USER_NAME, config.MONGODB_CONFIG.PASSWORD,mechanism='SCRAM-SHA-1')
# 连接表
collection = db.news
复制代码


def init():
    #创建连接对象 ''' mongodb://localhost:27017/ '''
    client = pymongo.MongoClient(host='127.0.0.1',port=27017)
    #指定数据集(数据库名称) ''' client['test'] '''
    db = client.testDB
    #指定集合 ''' db['students'] '''
    return db.students
#插入单条数据
def insert(collection,student):
    result = collection.insert(student)
    print(result)
#插入多条数据
def insert(collection,students):
    result = collection.insert(students)
    print(result)
#使用3.x推荐方法插入单条数据
def insert_one(collection,student):
    result = collection.insert_one(student)
    print(result)
    print(result.inserted_id)
#使用3.x推荐方法插入多条数据
def insert_many(collection,students):
    result = collection.insert_many(students)
    print(result)
    print(result.inserted_ids)
#查询单条数据
def find_one(collection,args):
    result = collection.find_one(args)
    print(type(result))
    print(result)
#查询多条数据
def finds(collection,args):
    results = collection.find(args)
    print(type(results))
    print(results)
    for result in results:
        print(result)
#查询记录数
def count(collection,args):
    count = collection.find(args).count()
    print(count)
#排序 ''' ASCENDING 升序  DESCENDING 降序 '''
def sort(collection,args,cending):
    results = collection.find().sort(args, cending)
    print([result[args] for result in results])
#偏移n位后查询
def skip(collection,index):
    results = collection.find().skip(index)
    print([result['name'] for result in results])
#获取n条记录
def limit(collection,index):
    results = collection.find().limit(index)
    print([result['name'] for result in results])
#更新数据库
def update(condition,student):
    result = collection.update(condition, student)
    print(result)
#删除数据 delete_one() delete_many()
def remove(collection,args):
    result = collection.remove(args)
    print(result)



相关实践学习
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
相关文章
|
2天前
|
Python
【Python操作基础】——帮助文档
【Python操作基础】——帮助文档
|
2天前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
2天前
|
Python
【Python操作基础】——集合
【Python操作基础】——集合
|
2天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
2天前
|
Python
【Python操作基础】——while语句用法和pass语句
【Python操作基础】——while语句用法和pass语句
|
3天前
|
Python
【Python操作基础】——if语句用法
【Python操作基础】——if语句用法
|
3天前
|
存储 Python
【Python操作基础】系列——赋值语句
【Python操作基础】系列——赋值语句
|
3天前
|
Python
【Python操作基础】——语句书写规范
【Python操作基础】——语句书写规范
|
3天前
|
Python
【Python操作基础】——变量操作
【Python操作基础】——变量操作
|
3天前
|
Python
【Python操作基础】——数据类型
【Python操作基础】——数据类型