Python's Pymongo

Source: Internet
Author: User

Before I had to tinker with the next MySQL. Think about it later. Python seems to be more suitable for use with NoSQL. And then I set up the next MongoDB to play.

It was a lot better than MySQL when I tried it. Needless to say, encapsulation encapsulation.

ImportPymongoclassMONGOHP:def __init__(self,addr,port,dbname,tablename=None): Self.conn=Pymongo. Mongoclient (addr,port) self.db=Self.conn[dbname] Self.tablename=tableName Self.dbname=dbName self.addr=addr Self.port=PortifTABLENAME:SELF.TB=Self.db[tablename]#Insert a piece of data    defInsert (self,data,tablename=None): MYTB=self.gettable (tableName)ifmytb:mytb.insert_one (data)returnTruereturnFalse#BULK INSERT Data    defInsertall (self,list,tablename=None): MYTB=self.gettable (tableName)ifMytb:mytb.insert (list)returnTruereturnFalse#the data is inserted when the condition is met, such as when inserting a value, it is judged that there is no current data in the table. Before inserting the    defInsertsig (self,reason,data,tablename): MYTB=self.gettable (tableName)ifMytb:mytb.update_one (reason,data)returnTrue#gets the specified data    defFind (self,data,tablename=None): MYTB=self.gettable (tableName)ifMYTB:returnmytb.find (data)returnFalse#If you need a special action, you can get the table out of action    defGetTable (self,tablename=None):ifTableName:returnSelf.db[tablename]ifSelf.tablename:returnSELF.TBreturnFalsedefRemove (self,reason=none,tablename=None): MYTB=self.gettable (tableName)ifMYTB:ifreason:mytb.remove (reason)Else: Mytb.remove ()returnTruereturnFalse#gets the specified number of table data    defCount (self,tablename=none,reason=none): MYTB=self.gettable (tableName)ifMYTB:ifreason:mytb.count (reason)Else:                returnMytb.count ()#Modify the default managed tables    defSettable (self,tablename=None): Self.tablename=tablename

Then test it casually. Very convenient. Home Essentials.

ImportMongotestdb=mongotest. MONGOHP ("xxxx.xxxx.xxxx.xxxx", 27017,"Mongodata","Mytable1") T1={"name":"Wang"}t2={"name":"Jiang"}t3={"name":"Li"}db.insert (t1) db.insert (T2) Db.insert (T3) datas= Db.find ({"name":"Wang"}) forIteminchdatas:Print(item) CNT=Db.count ()Print(CNT) db.remove (t1) CNT=Db.count ()Print(CNT) db.insert (T1,"Mytable2") CNT=db.count ("Mytable2")Print(CNT)

Python's Pymongo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.