gpt4 book ai didi

mongodb - 如何将MongoDb查询转换成csv(excel格式)

转载 作者:可可西里 更新时间:2023-11-01 10:10:34 25 4
gpt4 key购买 nike

这是我的查询。我想将此查询的输出导出为 csv(excel 格式),这样我就可以获得一个数据表。我怎样才能做到这一点?我正在使用 Robo3t。

(db.getCollection('sentimentOpinions').aggregate([ 
{ $match : { objectType : "Security" } },

{ $lookup:{
from: "securities",
localField: "objectId",
foreignField: "id",
as: "StockID" }},

{ $unwind:"$StockID" },

{ $lookup:{
from: "users",
localField: "userId",
foreignField: "userId",
as: "USER_ARJ" }},

{ $unwind:"$USER_ARJ" },

{$project :{USER_ID : "$userId",
NAME : { $concat: [ "$USER_ARJ.profile.firstName",", ",
"$USER_ARJ.profile.lastName" ] },
SECURITY_ID: "$StockID.id",
SECURITY: "$StockID.displayName",
TICKER: "$StockID.symbols.yahoo",
OPINION_VALUE: "$value",
OPINION_DATE: "$opinionDate"}},

{ $sort : { OPINION_DATE : -1 } } ]))

最佳答案

首先,您需要使用此查询为您的数据设置正确的格式,并使用 $out 将输出保存为 new_col 的新集合。

        (db.getCollection('sentimentOpinions').aggregate([ 
{ $match : { objectType : "Security" } },

{ $lookup:{
from: "securities",
localField: "objectId",
foreignField: "id",
as: "StockID" }},

{ $unwind:"$StockID" },

{ $lookup:{
from: "users",
localField: "userId",
foreignField: "userId",
as: "USER_ARJ" }},

{ $unwind:"$USER_ARJ" },

{$project :{USER_ID : "$userId",
NAME : { $concat: [ "$USER_ARJ.profile.firstName",", ",
"$USER_ARJ.profile.lastName" ] },
SECURITY_ID: "$StockID.id",
SECURITY: "$StockID.displayName",
TICKER: "$StockID.symbols.yahoo",
OPINION_VALUE: "$value",
OPINION_DATE: "$opinionDate"}},

{ $sort : { OPINION_DATE : -1 } },
{"$out" : "new_col"}
]))

现在您可以使用 mongoexport 以您想要的格式导出您的新收藏。

mongoexport --db db_name --collection new_col_name(new_col) --type csv --fields USER_ID,NAME,SECURITY_ID,SECURITY,TICKER,OPINION_VALUE,OPINION_DATE --out out_file.csv

关于mongodb - 如何将MongoDb查询转换成csv(excel格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51985453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com