pymongo 基本使用规则

# -*- coding: UTF-8 -*-
from pymongo import MongoClient

client = MongoClient('localhost', 27017)
# 获取数据库集合列表
# dbs = client.list_databases()
# for db in dbs:
#     print(db)
# 获取数据库下集合名称
# db_name = client.list_database_names()

#创建或链接数据库,如果数据库不存在则创建
# db = client['test']

# collectin = db['set'] # 创建集合

# post  = {
#     "job" : "SEO经理",
#     "age" : "35"
# }
#
# res = collectin.insert_one(post)

# l  = db.list_collection_names()  # 查看数据库seo下都有哪些集合

# es = db.drop_collection('set')  # 删除seo数据库下的set集合,若seo数据库下午其他集合则seo数据库也默认删除

# res = client.drop_database('test')  # 删除数据库test

db = client['seo']
collection = db['set']
# jihe1 = {
#     "title"  :  "春天",
#     "content": "花开了"
#         }
# jihe2 = {
#      "title" : "夏天",
#      "content":"雨来了"
#         }
# res  = collection.insert_many([jihe1,jihe2]).inserted_ids
# collection.delete_many({"title":"春天"})  # 删除所有title为春天的内容

collection.update_one({"title":"夏天"},{'$set':{"content":"更新,雨不来了"}})  # 更新一条  如更新多条update_many

res = collection.find_one({"title":"夏天"},{"content":1 ,"_id":0}) # 为1 为输出内容,0则为不输出内容

猜你喜欢

转载自blog.csdn.net/haohaomax1/article/details/110674439