出處: Youtube 彭彭的課程 Python Flask 網站後端開發 – MongoDB 新增資料
實作筆記
新增資料並且取的資料的ID
每一筆資料做成後都會有一個ID,這個ID是獨立不會重複的,這個ID到後期會很常用到,今天要來抓這個ID

把資料傳到一個變數result中,最後再去抓result的ID
#把資料放到資料庫中
db = client.mywebsite #選擇操作mywebsite資料庫
collection = db.users #選擇操作sers集合
#把資料新增到集合中
result = collection.insert_one({
"name":"Nini",
"number":"1",
"account":"go333",
"password":"123",
})
print("資料新增成功")
print(result.inserted_id)



新增多筆資料並且取的資料的ID
#把資料放到資料庫中
db = client.mywebsite #選擇操作mywebsite資料庫
collection = db.users #選擇操作sers集合
#把資料新增到集合中
result = collection.insert_many([{
"name":"Nini",
"number":"2",
"account":"go444",
"password":"123"
},{
"name":"Andy",
"number":"3",
"account":"go555",
"password":"123",
}])
print("資料新增成功")
print(result.inserted_ids)
資料是json格式,也是python的字典格式


