python连接mongodb简单操作

前言

只读环境,python当脚本,简单操作

正文

python连接mongodb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pymongo
from pymongo import MongoClient

import datetime
from datetime import timedelta
def test():
#建立连接
client=MongoClient("127.0.0.1",27017)
#连接所需要的数据库 test1111
testdb=client.test1111
#连接所需要的集合 goodsPrototype
testcollect=testdb.goodsPrototype
#向集合里添加一条新的数据
testcollect.insert({"id":"asdqwe","coverImgList":["12314","23213","22332"]})
#删除集合里的数据
testcollect.remove({"id":"asdqwe"})
#遍历集合里所有的数据
for item in testcollect.find():
print(item)
#修改集合里的数据
testcollect.update({"id":"GPZB2018101604ew"},{"$set":{"coverImgList":["open","down"]}})

if __name__ == '__main__':
test()
-------------本文结束感谢您的阅读-------------