当前位置: 首页 > 工具软件 > python-adc > 使用案例 >

Python-NoSQL数据库

阎佑运
2023-12-01

Python-NoSQL数据库 (Python - NoSQL Databases)

As more and more data become available as unstructured or semi-structured, the need of managing them through NoSql database increases. Python can also interact with NoSQL databases in a similar way as is interacts with Relational databases. In this chapter we will use python to interact with MongoDB as a NoSQL database. In case you are new to MongoDB, you can learn it in our tutorial here.

随着越来越多的数据变为非结构化或半结构化数据,通过NoSql数据库管理数据的需求增加了。 Python还可以以与关系数据库类似的方式与NoSQL数据库进行交互。 在本章中,我们将使用python与MongoDB作为NoSQL数据库进行交互。 如果您是MongoDB的新手,可以在这里的教程中学习它

In order to connect to MongoDB, python uses a library known as pymongo. You can add this library to your python environment, using the below command from the Anaconda environment.

为了连接到MongoDB,python使用了一个称为pymongo的库。 您可以使用Anaconda环境中的以下命令将此库添加到python环境中。


conda install pymongo

This library enables python to connect to MOngoDB using a db client. Once connected we select the db name to be used for various operations.

该库使python可以使用数据库客户端连接到MOngoDB。 连接后,我们选择要用于各种操作的数据库名称。

插入资料 (Inserting Data)

To insert data into MongoDB we use the insert() method which is available in the database environment. First we connect to the db using python code shown below and then we provide the document details in form of a series of key-value pairs.

要将数据插入MongoDB,我们使用在数据库环境中可用的insert()方法。 首先,我们使用下面显示的python代码连接到db,然后以一系列键值对的形式提供文档详细信息。


# Import the python libraries
from pymongo import MongoClient
from pprint import pprint

# Choose the appropriate client
client = MongoClient()

# Connect to the test db 
db=client.test

# Use the employee collection
employee = db.employee
employee_details = {
    'Name': 'Raj Kumar',
    'Address': 'Sears Streer, NZ',
    'Age': '42'
}

# Use the insert method
result = employee.insert_one(employee_details)

# Query for the inserted document.
Queryresult = employee.find_one({'Age': '42'})
pprint(Queryresult)

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


{u'Address': u'Sears Streer, NZ',
 u'Age': u'42',
 u'Name': u'Raj Kumar',
 u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}

更新数据 (Updating Data)

Updating an existing MongoDB data is similar to inserting. We use the update() method which is native to mongoDB. In the below code we are replacing the existing record with new key-value pairs. Please note how we are using the condition criteria to decide which record to update.

更新现有的MongoDB数据类似于插入。 我们使用mongoDB固有的update()方法。 在下面的代码中,我们将现有记录替换为新的键值对。 请注意,我们如何使用条件条件来决定要更新的记录。


# Import the python libraries
from pymongo import MongoClient
from pprint import pprint

# Choose the appropriate client
client = MongoClient()

# Connect to db
db=client.test
employee = db.employee

# Use the condition to choose the record
# and use the update method
db.employee.update_one(
        {"Age":'42'},
        {
        "$set": {
            "Name":"Srinidhi",
            "Age":'35',
            "Address":"New Omsk, WC"
        }
        }
    )

Queryresult = employee.find_one({'Age':'35'})

pprint(Queryresult)

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


{u'Address': u'New Omsk, WC',
 u'Age': u'35',
 u'Name': u'Srinidhi',
 u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}

删除资料 (Deleting Data)

Deleting a record is also straight forward where we use the delete method. Here also we mention the condition which is used to choose the record to be deleted.

删除记录也很简单,我们使用delete方法。 这里我们还提到了用于选择要删除的记录的条件。


# Import the python libraries
from pymongo import MongoClient
from pprint import pprint

# Choose the appropriate client
client = MongoClient()

# Connect to db
db=client.test
employee = db.employee

# Use the condition to choose the record
# and use the delete method
db.employee.delete_one({"Age":'35'})

Queryresult = employee.find_one({'Age':'35'})

pprint(Queryresult)

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。


None

So we see the particular record does not exist in the db any more.

因此,我们看到该特定记录不再存在于db中。

翻译自: https://www.tutorialspoint.com/python_data_science/python_nosql_databases.htm

 类似资料: