常用数据库:MongoDB

简单实例:

import pymongo  # 官方文档:https://api.mongodb.com/python/current/index.html
# 【连接MongoDB】
client = pymongo.MongoClient(host='localhost',port=27017)  # port默认参数为27017
# 【指定数据库】
db = client.test  # MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中
# 【指定Collection】
collection = db.students
# 【插入数据】
student = {
    'id': '123012015058',
    'name': 'handsome hj',
    'age': 23,
    'gender': 'male'
}
result = collection.insert_one(student).inserted_id
print(type(result), result)  # 注意_id并不是一个字符串
print(collection.find_one())  # 查询一个
print(collection.find_one({'name': 'handsome hj'}))  # 带值查询
result = collection.find({'name': 'handsome hj'})  # 查询多个,返回值为生成器
for i in result:
    print(i)
collection.delete_many({'name': 'handsome hj'})  # 删除操作

# 【更多Collection层面的操作请查阅:https://api.mongodb.com/python/current/api/pymongo/collection.html】

转载于:https://www.cnblogs.com/lokvahkoor/p/10763405.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值