Pymongo study 1th--additions and deletions

Source: Internet
Author: User
Tags mongoclient

Reference Documentation:

1, https://docs.mongodb.org/getting-started/python/

2, http://api.mongodb.org/python/current/api/pymongo/index.html

#-*-coding:utf-8-*-ImportSYS fromDatetimeImportdatetime fromPymongoImportmongoclientImportPymongoImportRedefMain ():" "Connect to database. Client = Mongoclient () is equivalent to client = Mongoclient ("localhost:27017")" "Client=mongoclient () DB=Client.demo Coll=db.address" "Insert Data. Insert_one () Insert_many ( )" "Insertresult=Coll.insert_one ({" Account":"[email protected]",        "Password":"123456",        "Birth": Datetime.strptime ("1990-09-22","%y-%m-%d"),        "score": 102,        "Address": {" City": ["Beijing","Wuxi","Chengdu"],"Country":" China"},        "Articles": [{"title":"Summary of common operation of MONGO","author":"Xiaomaque"},                    {"title":"MySQL Summary","author":"ponpon7"}]    })    Print(insertresult.inserted_id) elements= [{" Account":"ponpon7","Password":"Afi89^^r","Birth": Datetime.strptime ("1986-12-12","%y-%m-%d"),"score": 172}, {" Account":"Liudehua","Password":"Wangqingshui","Birth":"","score": 18}] Coll.insert_many (elements)" "Find or query data. Cursorall: Find score<140 data, and sort by account from small to large, score from large to small. $lt: Less than<; $GT: Greater than> cursoror:logical OR Cursorregex: Finding by regular expression" "Cursorall= Coll.find ({"score": {"$lt": 140}}). sort ([(" Account", Pymongo. Ascending), ("score", Pymongo. Descending)]) forDocumentinchCursorall:Print("All document: {}". Format (document)) Cursorregex= Coll.find ({" Account": Re.compile ('P', Re. I)}) forDocumentinchCursorregex:Print("Regex document: {}". Format (document)) Cursoror= Coll.find ({"$or": [{"score": 172}, {"Address.country":" China"}]})     forDocumentinchCursoror:Print("score is 172 Or country are China: {}". Format (document))" "Update data & replace data. Update_One (filter, UPDATE, Upsert=false, Bypass_document_validation=false) Upsert: If True, data is inserted when a document that matches the filter is not found , the default is False" "Updateresult=Coll.update_many ({" Account":"[email protected]"},        {            "$set": {"score": 129,"Address": {" City": ["Nanjing"],"Country":" China"}},            "$currentDate": {"lastmodified": True},#True    )    Print("Update:matched_count is {}". Format (updateresult.matched_count))Print("Update:modified_count is {}". Format (updateresult.modified_count)) Replaceresult=Coll.replace_one ({" Account":"[email protected]"},        {            " Account":"[email protected]",            "score": 139,            "Address": {" City": ["Paris"],"Country":"France"},        }    )    Print("Replace:matched_count is {}". Format (replaceresult.matched_count))Print("Replace:modified_count is {}". Format (replaceresult.modified_count))" "remove Data. Attintion:delete_many ({}) Delete all documents Coll.drop () drop a collection" "Deleteresult= Coll.delete_many ({"Birth": Datetime.strptime ("1990-09-22","%y-%m-%d")})    Print("Remove:deleted_count is {}". Format (deleteresult.deleted_count))if __name__=='__main__': Main ()

Pymongo study 1th--additions and deletions

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.