这是我的查询。我想将此查询的输出导出到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 } } ]))
答案 0 :(得分:2)
首先,您需要使用此查询为数据设置正确的格式,然后使用$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