栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Mongo与Python交互

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mongo与Python交互

交互再用管道 或者$ 的时候 要加引号括住
ret=collection.find({'$or':[{'age':{'$gt':20}},{'age':{'$lt':17}}]})
ret=collection.aggregate([{"$group":{'_id':'$age'}}])

from pymongo import MongoClient
client=MongoClient(host='localhost',port=27017)
collection=client['test']['py']
单条插入
collection.insert_one({'name':'zhangsan','age':20})
多条插入
collection.insert_many([{'name':'lisi','age':30},{'name':'wangwu','age':10}])
输出查询结果 是个生成器
ret=collection.find()

for x in ret:

    print(x)
先查询数据库中是否含有 {“name”:“test10005”}的数据,如果存在则 {“name”:“new_test10005”}进行修改,如果不存在则将 {“name”:“new_test10005”}插入到数据库
item_list=[{'name':'test1000{}'.format(i)} for i in range(1,10)]
print(item_list)

collection.update({"name":"test10005"},{"$set":{"name":"new_test10005"}},upsert=True)
删除一条数据
```python
#delete_one删除一条数据
collection.delete_one({"name":"test10010"})
删除全部数据
#delete_may删除所有满足条件的数据
collection.delete_many({"name":"test10010"})
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号