Python 操作 mongodb 的简单示例

2022-01-17  本文已影响0人  孙庚辛
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["FAQDB"]

mytable = mydb["faqs"]


mydict = {"_id": 0, "question":"晚上睡不着觉怎么办", "answer":"你可以在太阳穴抹一点薰衣草精油"}

ret = mytable.insert_one(mydict)
print(ret)
print(ret.inserted_id)

mylist = [{"_id":1, "question":"阳气不足怎么办", "answer":"睡觉的时候采用还阳卧"},{"_id":2, "question":"脸上的痘印颜色很重怎么办", "answer":"去医院用液氮冷冻"}]

ret = mytable.insert_many(mylist)
print(ret.inserted_ids)


myquery = {"_id":1}
mydoc = mytable.find(myquery)

for x in mydoc:
    print(x)

mytable.drop()
mytable.insert_one(mydict)
mytable.insert_many(mylist)

for x in mytable.find():
    print(x)
上一篇下一篇

猜你喜欢

热点阅读