pymongo 基础操作

本文将介绍如何使用Python的pymongo库连接MongoDB数据库,进行基本的数据增删查改操作,包括连接数据库、创建集合、插入文档、查询数据、更新记录和删除记录等核心功能。
摘要由CSDN通过智能技术生成

import pymongo

client = pymongo.MongoClient('mongodb://localhost:27017/')#连接数据库
db = client.test #等价于client['test'],创建test数据库
#指定集合
collection = db.students #等价于db['students']
#插入数据
student = {
    "id":"20170101",
    "name":"Jorden",
    "age":20,
    "gender":"male"
}
# result = collection.insert_one(student)
# # result = collection.insert_many([student1,student2])
# # print(result.inserted_ids)
# print(result.inserted_id)

#查询

# Query_result = collection.find_one({'name':'Jorden'})
# print(type(Query_result))
# print(Query_result)
# <class 'dict'>
# {'_id': ObjectId('623dbe2bcfbd0be74da88977'), 'id': '20170101', 'name': 'Jorden', 'age': 20, 'gender': 'male'}
#查询多条数据用find(),返回生成器对象,结果遍历
# Query_result = collection.find({'age':{
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值