将 mongodb/mongodump 中集合的指定字段导出到文件 [英] Export specified field of a collection in mongodb / mongodump to file

查看:478
本文介绍了将 mongodb/mongodump 中集合的指定字段导出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将单个指定字段转储到文件中.假设我有一个包含姓名、电子邮件和 ID 的集合用户,但我只想打印出字段 url 的所有内容,就像 mongod 中的这条语句:

I want to dump a single specified field to file. Let's say I have a collection users with name, email and id, but I only want to print out all contents of field url, like this statement in mongod:

db.users.find({}, {"email": 1});

现在我用查询选项尝试了 mongodump,但它不起作用:

Now I tried mongodump with the query option, but it doesn't work:

mongodump --db 'mydb' --collection 'users' --query '{}, {"email" : 1}'

基本上我只想将字段的内容放入可读文件中.有没有办法在没有脚本的情况下在命令行上执行此操作?

Basically I just want the contents of a field into a readable file. Is there a way to do it on the commandline without a script?

我自己找到了解决方案.我需要具有字段选项的mongoexport".

edit: I found a solution myself. I needed 'mongoexport' which has a fields option.

推荐答案

mongodump 不会给你一个可读的文件,它是 bson 格式.你想要 mongoexport:

mongodump will not give you a readable file, it's bson format. You want mongoexport:

mongoexport -d mydb -c users -f email --csv -o outputFile

这篇关于将 mongodb/mongodump 中集合的指定字段导出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆