编码实现txt文件导入mongodb_MongoDB to CSV/TXT,Mongodb查询内容输出到外部文件

方法一:

Every once in a while, I need to give a non-technical user (like a business analyst) data residing in MongoDB; consequently, I export the target data as a CSV file (which they can presumably slice and dice once they import it into Excel or some similar tool). Mongo has a handy export utility that takes a bevy of options, however, there is an outstanding bug and some general confusion as to how to properly export data in CSV format.

Accordingly, if you need to export some specific data from MongoDB into CSV format, here’s how you do it. The key parameters are connection information to include authentication, an output file, and most important, a list of fields to export. What’s more, you can provide a query in escaped JSON format.

You can find the mongoexport utility in your Mongo installation bin directory. I tend to favor verbose parameter names and explicit connection information (i.e. rather than a URL syntax, I prefer to spell out the host, port, db, etc directly). As I’m targeting specific data, I’m going to specify the collection; what’s more, I’m going to further filter the data via a query.

ObjectId’s can be referenced via the $oid format; furthermore, you’ll need to escape all JSON quotes. For example, if my query is against a users collection and filtered by account_id (which is an ObjectId), the query via the mongo shell would be:

Mongo Shell Query

[shell]

db.users.find({account_id:ObjectId('5058ca07b7628c0002099006')})

[/shell]

Via the command line à la monogexport, this translates to:

Collections and queries

[shell]

--collection users --query "{\"account_id\": {\"\$oid\": \"5058ca07b7628c0002000006\"}}"

[/shell]

Finally, if you want to only export a portion of the fields in a user document, for example, name, email, and created_at, you need to provide them via the fields parameter like so:

Fields declaration

[shell]

--fields name,email,created_at

Putting it all together yields the following command:

[/shell]

Puttin’ it all together

[shell]

mongoexport --host mgo.acme.com --port 10332 --username acmeman --password 12345 \

--collection users --csv --fields name,email,created_at --out all_users.csv --db my_db \

--query "{\"account_id\": {\"\$oid\": \"5058ca07b7628c0999000006\"}}"

[/shell]

Of course, you can throw this into a bash script and parameterize the collection, fields, output file, and query with bash’s handy $1, $2, etc variables.

方法二:

[shell]

mongo test --eval "printjson(db.getCollectionNames())"

[/shell]

方法三:

vim myjsfile.js

cursor = db.apps.find({'id':{$gt:"0"}},{'_id':0, 'id':1, 'name':1});

while ( cursor.hasNext() ) {

var row = cursor.next();

print( row['id'] + "\t" + row['name'] );

}

mongo localhost:27017/test myjsfile.js >> output.csv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值