MongoDB and Python with notes

Source: Internet
Author: User

Use Pymongo to connect to the MongoDB database in Python.

The basic code is as follows:

 fromPymongoImportmongoclientclient= Mongoclient ('127.0.0.1', 27017)# the address of MongoDBDb_name ='Myfirstmongo'                 #the name of the databasedb = Client[db_name]#now DB is the database.Collection = db['Testone']#' Testone ' is the name of the collection, and now collection is the collection we're going to use .

Query for a single piece of data and a simple paging operation:

ImportJSON fromBsonImportJson_util as JsonbdefGet_data_by_key (Key): Item= Collection.find ({"Key": Key})#find is the method of MONGO directly using set query    #the item returned by the Print item # query is an object that is printed out as <pymongo.cursor.cursor object at 0X7F5D9BBC6F50&G t;info = jsonb.dumps (item)#use Bjson to convert objects into readable form, now info is str typedata = Json.loads (info)#use JSON to convert a string to the appropriate type of Python, where data is a list of lists    returnDatadefget_page_data (page): Page_len= 20#Page_len is the length of a page and can also be passed in as a parameterSKP = (int (page)-1) *page_len#Skip is for querying only one page, skipping previous dataitem = Collection.find (). Skip (SKP). Limit (Page_len)#from SKP start, query Page_len datainfo = jsonb.dumps (item)#working with Datadata = Json.loads (info)#Working with Data
returnData
Get_data_by_key is a query by a certain condition, or a multi-criteria query. Note The data type of key.
Additions and deletions and other operations similar to the above process, just modify the collection corresponding operation function.
Delete data: Collection.find_one_and_delete ({' key ': key}), or Collection.remove ({' key ': Key})
use Remove () with caution, and when her parentheses are empty, all the data in the collection will be erased;
Add Data: Collection.insert ({' key ': ' 123 ', ' msg ': ' Test '}), you can insert multiple bars, put multiple data into the list, and pass the list into insert ();
Update data: collection.update ({' key ': ' 123 '},{' msg ': ' New '},true,true),
The update parameter means: The first is the query parameters, the second is to update the content, the third is to determine whether the data is not found when the creation, the fourth is to query to more than one data, whether to change;
Updating data can also use modifiers, which are not recorded here.

MongoDB and Python with notes

Related Article

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.