码迷,mamicode.com
首页 > 编程语言 > 详细

9.1 mongo_python.py

时间:2019-05-02 18:58:46      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:final   close   col   res   删除   owa   exce   imp   man   

# 安装 pymongo pip install pymongo

import pymongo

try:
    # 1.链接mongod的服务
    mongo_py = pymongo.MongoClient()
    # 2.库和表的名字; 有数据会自动建库建表
    # 数据库
    # db = mongo_py['six']

    # 表 集合
    # collection = db['stu']
    # collection = mongo_py['six']['stu']
    collection = mongo_py.six.stu

    # 3.插入数据
    one = {"name": "张三", "age": 50}
    two_many = [
        {"name": "小三", "age": 50},
        {"name": "李四", "age": 30},
        {"name": "王五", "age": 20},
        {"name": "小刘", "age": 15}
    ]

    # collection.insert_one(one)
    # collection.insert_many(two_many)
    # collection.insert()

    # 删除数据
    # collection.delete_one({"age": 15})
    # collection.delete_many({"age": 50})

    # 修改数据
    # collection.update({"age": 20}, {"$set": {"name": "小王"}})
    # collection.update_many({"name": "xiaowang"}, {"$set": {"age": 100}})

    #查询
    result = collection.find({"age":100})
    result = collection.find_one({'age':100})
    # for i in result:
    #     print(i)
    print(result)
except Exception as e:
    print(e)

finally:
    # 关闭数据库
    mongo_py.close()

9.1 mongo_python.py

标签:final   close   col   res   删除   owa   exce   imp   man   

原文地址:https://www.cnblogs.com/hankleo/p/10802726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!