Pymongo庫是Python操作mongodb的庫。下面簡單介紹Pymongo的簡單使用。
上述連接可以連接不設置密碼的mongo,但是mongo帶密碼就需要其他的連接方式了。
#SCRAM-SHA-1方式uri = "mongodb://user:[email protected]/the_database?authMechanism=SCRAM-SHA-1"client = MongoClient(uri)#或者client = MongoClient('example.com')client.the_database.authenticate('user', 'password', mechanism='SCRAM-SHA-1')#MONGODB-CR 方式from pymongo import MongoClienturi = "mongodb://user:[email protected]/the_database?authMechanism=MONGODB-CR"client = MongoClient(uri)#或者client = MongoClient('example.com')client.the_database.authenticate('user', 'password', mechanism='MONGODB-CR')創(chuàng)建索引可以加快查詢
collection.create_index([('user_id',pymongo.ASCENDING),] ,unique=True)#創(chuàng)建一個唯一的不可重復的索引user_id,一次可以創(chuàng)建多個索引參考官方文檔 http://api.mongodb.com/python/current/tutorial.html
|
新聞熱點
疑難解答