Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将MongoDb查询转换为csv(excel格式)_Mongodb_Export To Csv_Robo3t - Fatal编程技术网

如何将MongoDb查询转换为csv(excel格式)

如何将MongoDb查询转换为csv(excel格式),mongodb,export-to-csv,robo3t,Mongodb,Export To Csv,Robo3t,这是我的问题。我想将此查询的输出导出为csv(excel格式),这样我就可以有一个数据表。我该怎么做?我用的是机器人3T (db.getCollection('sentimentOpinions').aggregate([ { $match : { objectType : "Security" } }, { $lookup:{ from: "securities", localField: "objectId",

这是我的问题。我想将此查询的输出导出为csv(excel格式),这样我就可以有一个数据表。我该怎么做?我用的是机器人3T

(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 } } ]))

首先,您需要使用此查询对数据进行适当的格式设置,并使用将输出保存为新集合

现在,您可以使用导出新集合所需的格式


如果不想创建新集合并将查询的数据写入go-in csv文件,该怎么办?因为它会时不时地创建新的集合,这在硬盘内存方面是不可行的。
        (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 --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