Connecting the MongoDB database with the Pymongo module should be used like this:
Mongoclient
Mongoclient(hostport=Port)
DB = conn [DB] #连接指定数据库
DB [Col]. Find_one(x)
Db.spgoods.find_one (x)
DB [Col]. Update({"_id"spinfo["_id"] }True)
Db[Col]. Update({"_id": Spinfo["_id"]},{"$set":Spinfo},True)
. db[table]. Insert(spinfo)
DB [table]. Find()
You can also write a class pattern
classNoSQL:
def__init__( Self, host, port, DB):#主机地址, port, database name
Self. conn= Mongoclient(Host= HostPort=Port)
Self. db= Self. conn[Db]#连接指定数据库
def ( self , X) :
= [ "Spgoods" . Find_one ( x)
Spdata
defUpdatespgood( Self, Spinfo):
Self. db["Spgoods"]. Update({"_id": Spinfo["_id"]}, Spinfo,True)
True
NoSQL(' localhost '27017'goods ')
Then connect to the database layer to write this
defGetspinfo(Item,value,depart,comp):
Result= Pnosql.conn.getthespgood({Item: Value"Depart":Depart,"Comp": Comp})
getting this form of data requires a loop output
defGetspgoods( Self, table):
Spdata= Self. DB[Table]. Find()
returnSpdata
From for notes (Wiz)
Python connection Mongo