卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

使用Python在MongoDB中删除集合(如果存在的话)

使用Python在MongoDB中删除集合(如果存在的话)

MongoDB 是一种广泛流行的开源数据库,它以灵活的类JSON格式存储数据。它不使用传统的行列存储数据的技术。相反,它采用了一种更灵活的方法,增加了其可扩展性。

这个数据库被设计用来处理大量的数据,因此,它是专门为现代应用程序定制的。MongoDB数据库由“集合”组成,类似于关系型数据库中的表。

集合是由具有不同类型值的字段组成的文档组。一个数据库可以包含多个集合,每个集合可以包含多个文档。在本文中,我们将使用Python命令删除一个MongoDB集合。每个集合都有自己的模式,这取决于文档的结构。

安装 PyMongo

PyMongo 是Python驱动程序,程序员通过它与“MongoDB”数据库进行交互。它提供了一个接口,可以从Python中对MongoDB数据执行多个操作。我们可以通过使用命令行上的Python软件包管理器来安装“PyMongo”。

pip install pymongo 

一旦安装了PyMongo库,我们可以在本地IDE中导入它。

创建数据库

我们需要一个参考数据库来进行操作。创建一个MongoDB数据库并不是一项困难的任务。我们需要从互联网上下载最新版本的MongoDB并将其安装在系统上。之后我们将启动“MongoDB”服务器。我们可以使用默认服务器和默认端口号,并开始“连接”过程。我们可以通过传递数据库名称和集合名称来手动创建一个数据库。数据可以以JSON或CSV文件格式导入。

通过PyMongo将MongoDB连接到Python

这是最关键的一步,因为它涉及到在两个平台之间建立连接。我们将使用“pymongo.MongoClient()”函数来创建一个MongoClient 对象。通过将服务器地址作为参数传递给这个函数,就可以建立连接。

语法

Mongo_client = pymongo.MongoClient("Connection address") 

Let’s apply this method to establish a connection.

在Python中创建一个连接以读取集合

在这里,我们正在尝试读取存储在MongoDB中的集合。在下面给出的示例中 −

  • 我们导入了“PyMongo”库,并创建了一个“MongoClient”对象,它允许我们建立连接并访问数据库

  • 我们传递了一个服务器地址,将地址名称指定为“localhost”,这意味着MongoDB服务器正在与Python程序运行在同一台机器上。我们使用了MongoDB服务器的默认端口号:“27017”。

  • 在这之后,我们指定了数据库和集合的名称。

  • 我们已经创建了一个集合并填充了它。

  • 我们使用了“find()”方法来检索存储在集合中的文档。

Example

import pymongo
Mongo_client = pymongo.MongoClient("mongodb://localhost:27017/")

# Database name
database = Mongo_client["mydb"]

#Getting the database instance
database = Mongo_client['mydb']

#Creating a collection
collection = database['example']

#Inserting document into the collection
data = [{"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"},
{"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"},
{"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}]
res = collection.insert_many(data)
print("Data inserted ......")

#Retreving the data
documents = collection.find()
print("Contents of the collection: ")
for document in documents:
   print(document)

输出

Data inserted ......
Contents of the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}

现在,我们已经创建了一个数据库和一个集合,让我们来看一下从数据库中删除集合的方法。

使用Drop()方法删除集合

这是一种非常简单的方法,用于从数据库中删除集合。让我们来了解一下。

  • 建立连接后,我们使用 drop() 方法从数据库中删除了目标集合。

  • 一旦集合被删除,我们就无法使用“find()”方法来检索其文档。

  • 由于集合已被删除,因此输出为“None”。

Example

import pymongo
Mongo_client = pymongo.MongoClient("mongodb://localhost:27017/")

# Database name
database = Mongo_client["mydb"]

#Getting the database instance
database = Mongo_client['mydb']

#Creating a collection
collection = database['example']

documents = collection.find()
print("Contents of the collection: ")
for document in documents:
   print(document)

#dropping the collection
print(collection.drop())
print("Collection Dropped ......")

输出

F:Examples>python test.py
Contents of the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
None
Collection Dropped ......

如果您尝试打开MongoDB数据库并验证集合,您可以观察到该coll

结论

本文重点介绍了使用Python编程删除数据库中存在的“MongoDB”集合的简单操作。我们使用了“PyMongo”库来访问MongoDB数据库。我们建立了连接,并指定了目标数据库和集合名称。最后,我们使用了“drop()”方法从数据库中删除了该集合。

卓越飞翔博客
上一篇: 如何解决golang报错:cannot convert 'x' (type T) to type U: need type assertion,解决方案
下一篇: C++中的图形界面编程
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏