pip install pymongofrom pymongo import MongoClientclient = MongoClient('localhost', 27017)client.list_database_names()如果post_db不存在,则自动新建此数据库。
post_db = client.get_database('post_db')post_db.list_collection_names()如果post_collection不存在,则新建此集合。
post_collection = post_db.get_collection('post_collection')import datetime
post = {"author": 'zhangsan', 'text': '我的第一篇博客', "tags": ['mongodb', 'python', 'pymongo'],
"date": datetime.datetime.utcnow()}
post_collection.insert_one(post)post_collection.find_one()list = [{'author': 'lisi', "text": 'lisi text'}, {'author': 'wangwu', 'text': 'wangwu text'}]
post_collection.insert_many(list)for i in post_collection.find():
print(i)| 留言与评论(共有 0 条评论) “” |