pymongo

1. 安装

pip install pymongo

2. 建立连接

import pymongo

conn = pymongo.MongoClient(host='localhost', port=27017)
# 切换数据库
py = conn['python']
# 登录该数据库(需要的话)
#py.authenticate("python_admin", '123456')
# 简单来个查询
result = py["stu"].find()
for r in result:
    print(r)

3. 增删改查

import pymongo
from pymongo import MongoClient

#创建返回链接对象
def get_db(database):
    client = MongoClient(host="localhost", port=27017)
    db = client[database]
    return db


# 增删改查
# 增加数据
def add_one(table, data):
    db = get_db("python")
    result = db[table].insert_one(data)
    return result


def add_many(table, data_list):
    db = get_db("python")
    result = db[table].insert_many(data_list)
    return result.inserted_ids


def upd(table, condition, data):#condition条件
    db = get_db("python")
    result = db[table].update_many(condition, {'$set':data})
    return result


def delete(table, condition):#condition条件
    db = get_db("python")
    result = db[table].delete_many(condition)
    return result


if __name__ == '__main__':
    # r = add_one("stu", {"name": "西瓜", "age":18})
    # print(r.inserted_id)
    # r = add_many("stu", [{"name": "嘎嘎"},{"name": "咔咔"}])
    # print(r)

    # result = upd("stu", {"name": '西瓜'}, {"age": 100})
    # print(result)

    result = delete("stu", {"name": "嘎嘎"})
    print(result)

 

posted @ 2024-03-05 16:07  Crazy丶迷恋  阅读(7)  评论(0编辑  收藏  举报