Python to implement additions and deletions of MongoDB

Source: Internet
Author: User
Tags mongoclient mongodb mongodb server


python to implement additions and deletions of MongoDB


Environment:

192.168.122.1 python

192.168.122.11 MongoDB


First, the installation of Pip,python management tools

Official website Download: pip-18.0


Install PIP

[[email protected] pip-18.0]# python setup.py install

As below, the installation is successful

Note: When installing PIP, download Setuptools-39.2.0.zip unzip and run the Python setup.py install, otherwise there will be errors in the subsequent installation;


Second, Python connection MongoDB

Python connects MongoDB using the Pymongo module

Installing Pymongo

Pip Install Pymongo


Iii. installing the Python IDE tool

Official website Download pycharm-community-2018.2.1.tar.gz

Unzip to the specified directory, I designated as/usr/local/

In the/bin directory./pycharm.sh can be opened;


Note: When using pycharm, you need to add the module manually

File-to-setting-project interpreter click the plus sign to search for an installation (exit: Right-click Close)



Iv. Python connection Operation MongoDB

Increase


Import Pymongo//Importing module

From Pymongo import mongoclient

client= mongoclient (' 192.168.122.11 ', 27017)//LINK Database host

db = CLIENT.LH//connection LH database, not automatically created

My_set = db.test_set//Using Test_set collection, not automatically created

My_set.insert ({"Name": "Zhangsan", "Age": 18})//Insert Data


authentication, viewing in MongoDB server

As below, create a library, a collection, insert the data


As below, add more than one data

Users = [{' Name ': ' Lisi ', ' age ': 20},{' name ': ' Wangwu ', ' age ': 23}]
My_set.insert (Users)

View

Note: Each time it is run, it is added and does not overwrite the previously added data (for example: Zhangsan)


Change

Updating data with Update

My_set.update ({"Name": "Zhangsan"},{' $set ': {"age": 22}})


View:

The default changes the age of the first Zhang San;



Check

(1) All inquiries

For I in My_set.find ():

Print (i)


(2) Query the Name=zhangsan

For I in My_set.find ({"Name": "Zhangsan"}):
Print (i)


Print (My_set.find_one ({"Name": "Zhangsan"})


Conditional query

Query all records in the collection with age greater than 25

For I in My_set.find ({"Age": {"$GT": 20}}):

Print (i)



By deleting

Delete all records of Name=lisi

My_set.remove ({' name ': ' Lisi '})

As follows: Lisi Data deletion


Delete a record for an ID of Name=zhangsan

id = my_set.find_one ({"Name": "Zhangsan"}) ["_id"]
My_set.remove (ID)




Python to implement additions and deletions of MongoDB

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.