Idea中对MongoDB的简单增删查改

package com.martin.test;/*
 * @Author Martin·Antonio
 * @Description TODO
 * @DateTime 2021/6/15 16:08
 * */

import com.mongodb.BasicDBObject;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MongoApi {
    //1操作集合对象
    MongoCollection<Document> userTable = null;
    //2客户端对象
    MongoClient client = null;

    @Before
    public void init(){
        //3建立连接
        client = MongoClients.create("mongodb://192.168.67.138:27017");
        //4连接数据库
        MongoDatabase database = client.getDatabase("demo");
        //5获取要操作的集合
        userTable = database.getCollection("user");

    }
    @After
    public void close(){
        client.close();
    }


    @Test
    public void testQuery(){
        //查询所有
        FindIterable<Document> documents = userTable.find();
        for (Document document : documents){
            System.out.println(document);
        }
    }
    @Test
    public void testQuerywithCodetion(){
        //按条件查询
        Bson filter = Filters.gt("age",20);//gt=greater than,gte=greater than equal,lt=less than ,lte同上

        FindIterable<Document> documents = userTable.find(filter);

        for (Document document : documents){

            System.out.println(document);
        }
    }

    @Test
    public void testPage(){
        //按条件进行分页
        FindIterable<Document> documents = userTable.find().skip(2).limit(2);

        for (Document document:documents){

            System.out.println(document);

        }
    }
    //更新
    @Test
    public void testupdate(){

        userTable.updateOne(Filters.eq("_id",1), Updates.set("age",40));

    }
    //删除
    @Test
    public void testDelete(){

        userTable.deleteMany(new BasicDBObject());
        
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值