python中MongoDB的增删改查

1、说明

MongoDB使用查看数据的功能时非常方便。能立即看到增删改查的数据,无需操作命令行即可查看。

2、方法实例

(1)增

#增加一条记录
person={'name':'zone','sex':'boy'}
person_id=test.insert_one(person).inserted_id
print(person_id)

(2)删

#删除单条记录
result1=test.delete_one({'name':'zone'})
pprint.pprint(result1)

(3)改

#更新单条记录
res=test.update_one({'name':'zone'},{'$set':{'sex':'girlgirl'}})
print(res.matched_count)

(4)查

#查找多条记录
pprint.pprint(test.find())

#添加查找条件
pprint.pprint(test.find({"sex":"boy"}).sort("name"))

以上就是python中MongoDB增删改查的操作,希望对大家有所帮助。更多Python高级指路:python高级

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。