美文网首页
python和mongo的关联

python和mongo的关联

作者: 幼姿沫 | 来源:发表于2020-11-26 18:55 被阅读0次

python和MongoDB的联系 pymongo的操作

pymongo的创建步骤

1.从pymongo中导入MongoClient   

from pymongo  import MongoClient

2.实例化MongoClient对象  建立连接

client=MongoClient(host='127.0.0.1',port=27017)

3.连接数据库和集合    client[数据库名称][集合名称]

connect=client['mydb']['student']

4.往集合中给添加数据 插入一条记录,返回当前记录的id值

connect.insert({name:'haha',gender:'female'})

注意:往集合中添加多条数据用列表添加 插入多条数据

connect.insert([{name:'gaga',gender:'male'},{name:'lala',gender:'male'}])

# 插入多条数据data_list = [{"name":"test{}".format(i)} for i in range(10)]print(data_list)ret = collection.insert_many(data_list)print(ret)

[{'name': 'test0'}, {'name': 'test1'}, {'name': 'test2'}, {'name': 'test3'}, {'name': 'test4'}, {'name': 'test5'}, {'name': 'test6'}, {'name': 'test7'}, {'name': 'test8'}, {'name': 'test9'}]

<pymongo.results.InsertManyResult object at 0x000001B13458D400>

pycharm控制台显示 mongo数据库中的显示

相关文章

网友评论

      本文标题:python和mongo的关联

      本文链接:https://www.haomeiwen.com/subject/qevamktx.html