MongoDB使用(增删查改操作)

MongoDB使用(增删查改操作)

一、MongoDB创建数据库

1、MongoDB创建数据库的语法格式如下:

use DATABASE_NAME(数据库的名称)

使用该语句创建数据库时,如果数据库不存在,则创建数据库,否则切换到指定数据库。
例:创建user数据库

> use user
switched to db user
> db
user

2、使用“show dbs”语句可以查看所有的数据库(无数据的数据库不显示)

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
user    0.000GB

3、使用"db.database_name.insert()"可以想数据库中插入数据(database_name指我们想要插入数据的数据库的名称)

> db.user.insert({"name":"你好"})
WriteResult({ "nInserted" : 1 })

MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。
注意: 在 MongoDB 中,集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建。

二、MongoDB删除数据库

1、MongoDB 删除数据库的语法格式如下:

db.dropDatabase()

删除的是当前数据库,默认为 test,你可以使用 db 命令查看当前数据库名。
例:

> show dbs  		\\查看所有数据库
admin   0.000GB
config  0.000GB
local   0.000GB
user    0.000GB
> use user  		\\切换到数据库user
switched to db user
> db.dropDatabase()  		\\删除user数据库
{ "dropped" : "user", "ok" : 1 }
> show dbs			\\查看user数据库是否删除成功
admin   0.000GB
config  0.000GB
local   0.000GB

三、MongoDB创建集合

1、MongoDB 创建集合的语法格式如下:

db.createCollection(name, options)

参数说明:

  • name: 要创建的集合名称
  • options: 可选参数, 指定有关内存大小及索引的选项

options 可以是如下参数:
在这里插入图片描述
在插入文档时,MongoDB 首先检查固定集合的 size 字段,然后检查 max 字段。
例:
(1)在user数据库中创建runoob集合:

> use user
switched to db user
> db.createCollection("runoob")
{ "ok" : 1 }

(2)查看user数据库中的集合:

> show collections
runoob
> show tables
runoob

(3)创建固定集合mycol,整个集合空间大小为6142800B,文档最大个数为10000个:

> db.createCollection("mycol",{capped:true,autoIndexId:true,size:6142800,max:1000})
{
        "note" : "the autoIndexId option is deprecated and will be removed in a future release",
        "ok" : 1
}

> show tables
mycol
runoob

(4)在MongoDB中,我们不需要创建集合,当插入一些文档时,MongoDB会自动创建集合。

> db.mycol1.insert({"password":"12345"})
WriteResult({ "nInserted" : 1 })
> show tables
mycol
mycol1
runoob

四、MongoDB删除集合

MongoDB 中使用 drop() 方法来删除集合。
语法格式:

db.collection_name.drop()

如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。
实例
删除user数据库中的mycol1集合:

> db.mycol1.drop()		\\删除mycol1
true
> show tables		\\查询user数据库里面的集合,查看mycol1集合是否删除成功
mycol
runoob
> db.mycol1.drop() 		\\已经删除了mycol1,再次删除时删除失败
false

五、MongoDB插入文档

文档的数据结构和 JSON 基本一样。所有存储在集合中的数据都是 BSON 格式。BSON 是一种类似 JSON 的二进制形式的存储格式,是 Binary JSON 的简称。
1、MongoDB向集合中插入文档语句:

db.collection_name.insert()
db.collection_name.save()
db.collection_name.insertOne()		\\用于向集合中插入一个新文档
db.collection_name.insertMany()	 \\用于向集合中插入一个/多个文档
  • save():如果 _id 主键存在则更新数据ÿ
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较复杂的问题,我会尽量帮你解答。 首先,我们需要使用 Python 爬取奥运会数据。可以用 requests 库向目标网站发送 HTTP 请求,然后用 BeautifulSoup 库解析 HTML 页面,从而获取需要的数据。具体操作步骤如下: ```python import requests from bs4 import BeautifulSoup # 发送 HTTP 请求获取 HTML 页面 url = 'https://olympics.com/tokyo-2020/olympic-games/en/results/all-sports/medal-standings.htm' response = requests.get(url) # 解析 HTML 页面获取需要的数据 soup = BeautifulSoup(response.text, 'html.parser') table = soup.find('table', class_='medal-standing-table') rows = table.find_all('tr') for row in rows[1:]: cols = row.find_all('td') country = cols[1].text.strip() gold = cols[2].text.strip() silver = cols[3].text.strip() bronze = cols[4].text.strip() print(country, gold, silver, bronze) ``` 上面的代码可以爬取奥运会奖牌榜的数据,并打印出来。接下来,我们需要将获取的数据导入到 MongoDB 数据库中。可以使用 PyMongo 库来连接 MongoDB,然后将数据插入到指定的集合中。具体操作步骤如下: ```python import pymongo # 连接 MongoDB 数据库 client = pymongo.MongoClient('mongodb://localhost:27017/') db = client['olympics'] medal_collection = db['medal'] # 插入数据到集合中 for row in rows[1:]: cols = row.find_all('td') country = cols[1].text.strip() gold = cols[2].text.strip() silver = cols[3].text.strip() bronze = cols[4].text.strip() medal_collection.insert_one({ 'country': country, 'gold': gold, 'silver': silver, 'bronze': bronze }) ``` 上面的代码可以将奥运会奖牌榜的数据插入到 MongoDB 的 medal 集合中。接下来,我们可以通过 PyMongo 库提供的 API 来实现增删查改操作。具体操作步骤如下: ```python # 查询所有数据 cursor = medal_collection.find() for document in cursor: print(document) # 查询指定条件的数据 query = {'country': 'China'} cursor = medal_collection.find(query) for document in cursor: print(document) # 更新指定条件的数据 query = {'country': 'China'} new_values = {'$set': {'gold': '38'}} medal_collection.update_one(query, new_values) # 删除指定条件的数据 query = {'country': 'China'} medal_collection.delete_one(query) ``` 上面的代码分别演示了查询所有数据、查询指定条件的数据、更新指定条件的数据和删除指定条件的数据的操作。 希望这些代码可以帮助你完成 Python 爬取奥运会数据并导入 MongoDB 进行增删查改等功能的任务。如果还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值