How to connect to mongodb using python

Source: Internet
Author: User
Tags how to connect to mongodb
This article describes how to use python to connect to mongodb and how to install python to connect to the mongodb library file pymongo

Pip install pymongo

Connect to the mongodb program using python

Import pymongo conn = pymongo. consumer client ("ip", port) db = conn. admin # connect to the database. authenticate ("account", "password") # User Authentication db = conn. jwhdb. test. insert ({'id': 1, 'name': 'Kaka ', 'Sex': 'male'}) # insert a data = db. test. find () # print all data for I in data: print (I)

Run the program. The result is as follows:

C: \ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python35-32 \ python.exe D:/Users/Administrator/PycharmProjects/untitled/test/conn_mongodb.py {'1': 1.0, '_ id': ObjectId ('58c13ed1b5ad0ac03e6cbb80')} {'Sex': 'male', 'name': 'Kaka ',' _ id ': objectId ('58cb7dd7c720b21eb011db7d '), 'id': 1} {'Sex': 'male', 'name': 'Kaka ',' _ id ': objectId ('58cb8140c720b222ec46f729'), 'id': 1}

The above describes how to connect to and operate mongodb using python. For more information, see other related articles in the first PHP community!

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.