在命令窗口先打开mongo,创建一个库,比如use.tomatoues
并创建集合tomatoues.createcollection(tudou)
下面是代码:
import pymongo
if __name__ == ‘__main__’:
# 往 tomatoues数据库的 lagou 集合 写入一条任意记录
#和mongodb数据库进行连接
client = pymongo.MongoClient(‘localhost’, port=27017)
#得到 数据库
db = client.tomatoues
#得到集合
coll = db.tudou
#写入数据 name=terry, ag=18
coll.insert({‘name’: ‘terry’, ‘age’: 18})**
本文详细介绍如何使用Python和pymongo库连接MongoDB数据库,包括创建数据库、集合及插入数据的具体步骤。通过实例演示,读者可以快速掌握利用Python进行MongoDB数据库操作的方法。

1万+

被折叠的 条评论
为什么被折叠?



