MongoDB:导出不同的列值 [英] MongoDB: export distinct column value

查看:69
本文介绍了MongoDB:导出不同的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何使用 mongoexport 命令在 mongodb 上导出不同的列值.

Anyone have idea how to export distinct column value on mongodb using mongoexport command.

命令: db.coll.distinct("uid");

Command: db.coll.distinct("uid");

我尝试过的解决方案是:

The solution i have tried is :

 mongoexport --csv -d db -c collection -q '{distinct: "collection", "key": "uid"}' -f "uid" -o distinctUid.csv

推荐答案

这是不可能的.distinct 是数据库命令,--query 选项只允许使用 query 来过滤文档.

It's not possible to do it this way. distinct is a database command, and --query option allows to use only a query to filter documents.

如果您使用的是 mongodb v3.4 或更高版本,您可以使用 views聚合您的收藏并将其从视图中导出.

If you are using mongodb v3.4 or later, you can use views to aggregate your collection and export it from the view.

首先在 mongo shell 中创建视图:

First create the view in mongo shell:

db.createView("distinctCollectionUid", "collection", [{ $group: { _id: "$uid"}}]);

然后导出:

mongoexport -d db -c distinctCollectionUid --type=csv -f "_id" -o distinctUid.csv

这篇关于MongoDB:导出不同的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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