Python MongoDB operations and Instances

Source: Internet
Author: User

Python MongoDB operations and Instances

Python MongoDB operation details and examples

Because the data in the MongoDB library needs to be displayed on the page, you need to use python to operate MongoDB. The PyMongo module is an interface package for Python to operate on MongoDB. Therefore, the home page is installed with pymongo.

1. Installation command

pip install pymongo

2. query command:

Import pymongo # create a connection client = pymongo. export client (host = "10.0.2.38", port = 27017) # connect to the probeb database db = client ['probeb'] # print all set names in the database print (db. collection_names () # connect to the collection of test1 = db. test1 # This command is used to search for data with a value greater than srssi and a value greater than stime and greater than stime. Data with a value smaller than etime is sorted by stime flashback and sumdata = collection. find ({"Arg": {"$ gt": int (srssi), "$ lt": int (erssi)}, "stime": {"$ gt ": stime, "$ lt": etime }}). sort ([('stime ',-1)]) # This command is used to find if the received signal strength is greater than srssi and less than erssi, stime is greater than stime and less than etime, and mac is equal to search or dmac is equal to search (search is a variable, "$ options": "I" is used to ensure that the search content is case-insensitive, sort sumdata = collection in stime flashback. find ({"Arg": {"$ gt": int (srssi), "$ lt": int (erssi)}, "stime": {"$ gt ": stime, "$ lt": etime}, "$ or": [{"mac": {"$ regex": search, "$ options": "I "}}, {"dmac": {"$ regex": search, "$ options": "I"}]}). sort ([('stime',-1)]) # The current query result is assigned to sumdata. to find specific data, you can use the for loop for data in sumdata: print (data) # Note: you cannot use db when operating MongoDB for sorting in python. test1.find (). sort ({"name": 1, "age": 1}) # Otherwise, the following exception occurs: # TypeError: if no direction is specified, key_or_list must be an instance of list # solution: # db. tes1t. find (). sort ([("name", 1), ("age", 1)]) # reason: in python, you can only sort the list, not the dictionary.

3. insert data

Import datetime

# Insert data account = {"AccountID": 1, "UserName": "libing", 'date': datetime. datetime. now ()} accounts = [{"AccountID": 2, "UserName": "liuw", 'date': datetime. datetime. now ()}, {"AccountID": 3, "UserName": "urling", 'date': datetime. datetime. now ()}] # The insert time of each record is collections. insert (account)

4. All in all, python commands for MongoDB and MongoDB are similar. As long as you are familiar with MongoDB command operations, using pymongo is not a problem.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.