pymongo模块

python

from pymongo import MongoClient

1.链接

client=MongoClient('mongodb://root:123@127.0.0.1:27017/')

2.use 数据库

db  =client['db']等同于:client.db1

3.查看库下所有集合

print(db.collenction_names(include_system_collections=False))

4.创建集合

table_user = db['userinfo']

5.插入文档

import datetime

user0= {"_id":1,"name":"egon","birth":datetime.datetime.now(),"age":10}

res = table_user.insert_many([user0,user1]).inserted_ids

6.查找

from pprint import pprint格式化

pprint(table_user.findOne())

for item in table_user.find():

  pprint(item)

7.更新

table_user.update({'_id';1},{'name':'egon''})

8.传入新的文档替换旧的文档

table_user.save({"_id":2,"name":"sb"})

上一篇:使用C#Driver以名称打开MongoDB GridFS


下一篇:python – 批量upsert不工作pymongo