使用bat脚本以json格式导出mongo数据库中指定表数据

问题:

使用bat脚本以json格式导出mongo数据库中指定表数据,简单记录一下。

解决:

@echo on
set mongo_bin=D:/MongoDB/Server/4.2/bin
set db=A2DB_S_PID
set tableStand=_Standard_List
set table=_S_10b450c8-325b-43ba-8a1d-623d9bedbb82
set ip=10.0.0.16
set port=27700
set /a port1=%port%+1
set /a port2=%port%+2
set username=admin
set password=5382DF4748760FA5ABD91213B1EF4621E7DD8002333121170912B42922823FEE
set outPath=D:/A2DB_S_PID

cd /d %mongo_bin%
mongoexport --host "A2DB/%ip%:%port%,%ip%:%port1%,%ip%:%port2%" --authenticationDatabase admin -u %username% -p %password% --collection %tableStand% --db %db% --out %outPath%/%tableStand%.json
mongoexport --host "A2DB/%ip%:%port%,%ip%:%port1%,%ip%:%port2%" --authenticationDatabase admin -u %username% -p %password% --collection %table% --db %db% --out %outPath%/%table%.json
echo OK!!!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个简单的示例代码,可以参考一下: ```python import scrapy import pymongo class DoubanTop250Spider(scrapy.Spider): name = 'douban_top250' start_urls = ['https://movie.douban.com/top250'] def parse(self, response): # 遍历每个电影条目 for movie in response.css('.item'): # 解析电影信息 title = movie.css('.title::text').get() rating = movie.css('.rating_num::text').get() year = movie.css('.bd p::text').re_first(r'\d{4}') directors = movie.css('.bd p::text').re(r'导演: (.+?) ') stars = movie.css('.bd p::text').re(r'主演: (.+?) ') # 将电影信息存入 MongoDB self.save_to_mongo(title, rating, year, directors, stars) # 获取下一页的链接,递归调用 parse 方法 next_page = response.css('.next a::attr(href)').get() if next_page: yield response.follow(next_page, self.parse) def save_to_mongo(self, title, rating, year, directors, stars): # 连接 MongoDB client = pymongo.MongoClient('mongodb://localhost:27017/') db = client['douban'] collection = db['top250'] # 构造电影数据 movie = { 'title': title, 'rating': rating, 'year': year, 'directors': directors, 'stars': stars } # 插入数据 collection.insert_one(movie) ``` 在这个示例,我们定义了一个名为 `DoubanTop250Spider` 的 Scrapy 爬虫,在 `parse` 方法遍历豆瓣电影 top250 页面的每个电影条目,使用 CSS 选择器解析电影信息,并将数据存入 MongoDB 数据库。 具体来说,我们使用了 PyMongo 库连接到本地 MongoDB 数据库,定义了名为 `douban` 的数据库和名为 `top250` 的集合,构造了电影数据,并使用 `insert_one` 方法将数据插入集合。 除此之外,我们还使用了 Scrapy 的 `response.follow` 方法获取下一页的链接,并递归调用 `parse` 方法,直到遍历完所有电影条目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值