pymongo基础

PyMongo是MongoDB数据库的python模块

MongoDB是由C++语音编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统。

win10 安装 4.0

使用官网的配置

 

使用 net start mongodb 查看是否启动成功

 

 

import pymongo
client = pymongo.MongoClient("localhost",27017) #连接数据库
test = client["test"] #使用test数据库
students = test["students"] #使用students表格

#数据
def data_portion():
    insert_data_1 = {"id":"01",
                   "name":"小明",
                   "age":12}
    insert_data_2 = {"id":"02",
                   "name":"小红",
                   "age":11}
    insert_data_3 = {"id":"03",
                   "name":"小强",
                   "age":12}
    data = [insert_data_1, insert_data_2, insert_data_3]
    return data
#插入数据
def insert_portion(data):
    students.insert_many(data) #插入数据
    results = students.find() #查看所以数据,返回一个生成器对象
    for result in results:
        print(result["name"])
    print("当前数据一共有: " + str(students.count()) + "")

#删除数据
def remove_portion():
    print("remove------------------------------")

    remove_dates = students.find({"age":{"$ne":12}}) # $ne:不等于
    print("匹配条数: " + str(remove_dates.count()) + "")
    for remove_date in remove_dates:
        students.delete_one(remove_date)

#查找数据
def find_portion():
    print("find------------------------------")
    count = students.find().count()
    print("数据一共有: " + str(count) + "")
    finds = students.find()
    print("分别是:")
    for find in finds.sort("id",pymongo.ASCENDING):
        print(find)

#排序截取数据
def sort_skip_portion():
    print("sort_skip------------------------------")
    #排序 : sort(按照谁排序,排序方式)。升序:ASCENDING,降序:DESCENDING
    #偏移 : skip(偏移数量) #限制 : limit(限制个数)
    results = students.find().sort("id",pymongo.ASCENDING).skip(2).limit(2)
    print([result["name"] for result in results]) #遍历并以列表方式储存

#更新数据
def update_portion():
    # update用法官网不推荐使用
    # update语句需要 $ 操作
    print("update------------------------------")

    def first_find_portion():
        # update_one的使用
        find = {"id":"01"}
        replace = {"$set":{"id":"clear"}}
        # update(替换对象,替换内容)
        update = students.update_one(find,replace)
        print("匹配的条数: " + str(update.matched_count))
        print("影响的条数: " + str(update.modified_count))
    def second_find_portion():
        # update_many的使用
        find = {"id":{"$nin":["clear","01"]}} # $nin : 不在范围
        replace = {"$set":{"id":"be update"}}
        update = students.update_many(find,replace)
        print("匹配的条数: " + str(update.matched_count))
        print("影响的条数: " + str(update.modified_count))

    first_find_portion()
    second_find_portion()
if __name__ == "__main__":
    # 清空数据库数据
    students.delete_many({"id":{"$ne":0}})
    # 循环插入3次
    for i in range(3):
        # 调用函数
        insert_portion(data_portion())
    remove_portion()
    sort_skip_portion()
    update_portion()
    find_portion()

下面为运行结果:

转载于:https://www.cnblogs.com/py-peng/p/10700371.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值