Home > Backend Development > Python Tutorial > Python server programming: MongoDB database usage guide

Python server programming: MongoDB database usage guide

王林
Release: 2023-06-18 10:25:54
Original
1651 people have browsed it

Python server programming: MongoDB database usage guide

MongoDB is a NoSQL database. Compared with traditional relational databases, it has obvious advantages in certain scenarios. This article will introduce how to use the MongoDB database on the Python server side, including installation, connection, basic operations and query optimization.

1. Install MongoDB database

The MongoDB official website provides installation packages for various operating systems. Here we choose to install on Ubuntu. Open the terminal and enter the following command:

sudo apt-get install mongodb
Copy after login

After the installation is complete, start the MongoDB service:

sudo service mongodb start
Copy after login

We can verify whether the service has been started by using the following command:

sudo systemctl status mongodb
Copy after login

If the service has been started When started, the following information will be displayed:

● mongodb.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-06-21 15:50:15 UTC; 49s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 7720 (mongod)
    Tasks: 23 (limit: 1137)
   Memory: 75.4M
   CGroup: /system.slice/mongodb.service
           └─7720 /usr/bin/mongod --config /etc/mongodb.conf
Copy after login

2. Connect to MongoDB database

Python officially provides the pymongo module, which can support the connection and operation of Python and MongoDB. We can connect to MongoDB through the following code:

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
Copy after login

In the above code, "localhost" refers to the local host address, and the IP address of other hosts can also be used. Next, we can specify the database to use:

db = client["database_name"]
Copy after login

Among them, "database_name" can be any name. If the database does not exist, it will be created automatically.

3. Basic operations

  1. Insert data

We can insert data into the collection through the following code:

collection = db["collection_name"]
data = {"name": "Alice", "age": 20}
collection.insert_one(data)
Copy after login
  1. Query data

We can query the data in the collection through the following code:

for data in collection.find():
    print(data)
Copy after login

The above code will query all the data in the collection.

  1. Update data

We can update the data in the collection through the following code:

query = {"name": "Alice"}
new_value = {"$set": {"age": 21}}
collection.update_one(query, new_value)
Copy after login

The above code will update the record named "Alice" The "age" field is 21.

  1. Delete data

We can delete the data in the collection through the following code:

query = {"name": "Alice"}
collection.delete_one(query)
Copy after login

The above code will delete the record named "Alice".

4. Query Optimization

MongoDB has powerful query functions and can perform complex queries through various options. Below we’ll cover a few of the important options.

  1. limit option

We can limit the number of records returned by the query through the limit option. The following code will return the first 5 records in the collection:

result = collection.find().limit(5)
for data in result:
    print(data)
Copy after login
  1. sort option

We can sort the query results through the sort option. The following code will return records sorted by the "age" field in ascending order:

result = collection.find().sort("age")
for data in result:
    print(data)
Copy after login

The above code will return records sorted by the "age" field in descending order:

result = collection.find().sort("age", -1)
for data in result:
    print(data)
Copy after login
  1. skip option

We can skip the first several records of the query results through the skip option. The following code will return the records starting from the 5th record in the query results:

result = collection.find().skip(4)
for data in result:
    print(data)
Copy after login

The above is the basic usage of MongoDB database in Python server programming and the optimization query method. For more query optimization methods, readers can refer to the official MongoDB documentation.

The above is the detailed content of Python server programming: MongoDB database usage guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template