MongDB是一種非關(guān)系型數(shù)據(jù)庫(NoSQL),非關(guān)系型數(shù)據(jù)庫是以key-value的形式展現(xiàn)的,相比關(guān)系型數(shù)據(jù)庫來說,存儲容易,那么Python如何操作MongDB呢?
首先要確保是否安裝,如果已安裝進(jìn)行連接即可,下面是一些CURD操作(增刪改查):
·插入一條數(shù)據(jù)insert_one()
test={"name":"zhangsan","age":"25","sex":"man"}
run1.insert_one(test)
·插入一條數(shù)據(jù)insert_many()
testone={"name":"zhangsan","age":"25","sex":"man"};
testtwo={"name":"lisi","age":"20","sex":"woman"};
list=[testone,testtwo];
run1.insert_many(list);
·刪除一條數(shù)據(jù)delete_one()
#刪除name字段值為"lisi"
run1.delete_one({"name":"lisi"});
·刪除多條數(shù)據(jù)delete_many()
#刪除name字段值為"zhangsan"的多條數(shù)據(jù)
run1.delete_many({"name":"zhangsan"});
·刪除集合【表】drop()
run1.drop();
·修改一條數(shù)據(jù)update_one()
#前面是查詢的條件也就是需要更改的條件后面是需要修改的內(nèi)容
run1.update_one({"name":"lisi"},{"$set":{"age":"30"}});
·修改多條數(shù)據(jù)update_many()
#修改以w開頭的sex字段的所有數(shù)據(jù)換成性別為woman
one={"sex":{"$regex":"^w"}};
two={"$set":{"sex":"man"}};
run1.update_many(one,two);
·查詢一條數(shù)據(jù)find_one()
list={"name":"zhangsan","sex":"man"};
run1.find_one(list);
·查詢多條數(shù)據(jù)find()
forfindoneinrun1.find({"sex":"woman"})