mongoexport 导出 不重复的列 distinct 的方法

如何使用mongoexport 导出distinct 不重复的column value 值

MongoDB: export distinct column value


由 余生颓废 提交于 2021-02-07 18:22:32
问题

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

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

回答1:

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.

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

First create the view in mongo shell:

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

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

回答2:

its working for me.

Example:

First create view:

db.createView("distinctCollectionBI2_Style( BI2_Style is  key which value must distinc)","best_seller_data(your collection name)", [{ $group: { _id: "$BI2_Style"}}]);
Then export:

mongoexport -d best-seller(db name) -c distinctCollectionBI2_Style --type=csv -f "_id" -o Desktop/pretr/style.csv


来源:https://stackoverflow.com/questions/46560326/mongodb-export-distinct-column-value
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值