一個Python操做mongodb的模塊mongodb
# coding=utf-8 from pymongo import MongoClient #實例化client,創建鏈接 client = MongoClient(host="127.0.0.1",port=27017) # 不存在會自動建立 collection = client["test"]["try"] #插入一條數據 # ret1 = collection.insert({"name":"xiaowang","age":10}) # print(ret1) #插入多條數據 # data_list = [{"name":"test{}".format(i)} for i in range(10)] # collection.insert_many(data_list) #查詢一個記錄 # t = collection.find_one({"name":"xiaowang"}) # print(t) #查詢全部記錄 t = collection.find({"name":"xiaowang"}) print(t) # for i in t: # print(i) # # for j in t: # print(j,"*"*100) print(list(t))