将mongo查出的数据导出来,变成json,然后转Excel

在MongoDB shell或使用命令行工具(如mongo或mongosh)中,你可以将查询结果输出到JSON文件。以下是一个示例命令,它执行上述聚合查询并将结果写入名为output.json的文件:

mongo your_database_name --quiet --eval 'db.user.aggregate([...]).toArray()' > output.json

例如:

mongo cm-sites-sys --quiet --eval 'db.user.aggregate([{$match: {}},{$lookup: {from: "dept", localField: "deptIds", foreignField: "_id", as: "depts" }},{$unwind: "$depts" },{$project: {name: 1, account: 1, deptName: "$depts.name" }}]).toArray()'  > output.json

导出后

使用Excel  转

json数据转成excel / excel转json_json格式转换成excel-CSDN博客

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

Java-请多指教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值