2017-05-08 19 views
0

を私は下図のようMongoの文書(taskExecution)を有する:のMongoDB:投影において、アレイ内のspecifc要素をフィルタ

{ 
    "_id" : "46647-1493064966032-0-1", 
    "_class" : "net.afferolab.hub.microservice.model.TaskExecution", 
    "name" : "PEOPLE", 
    "executionDate" : ISODate("2017-04-24T20:32:22.722Z"), 
    "taskInformation" : [ 
     { 
      "status" : "BEGIN", 
      "message" : "Iniciando rota de integração de pessoas", 
      "informationDate" : ISODate("2017-04-24T20:32:22.730Z") 
     }, 
     { 
      "status" : "INFO", 
      "message" : "[ ] - Obtendo arquivo do servidor FTP.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.090Z") 
     }, 
     { 
      "status" : "INFO", 
      "message" : "[X] - Obtendo arquivo do servidor FTP.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.097Z") 
     }, 
     { 
      "status" : "ERROR", 
      "message" : "[ ] - Upload do arquivo para o S3.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.102Z") 
     }, 
     { 
      "status" : "INFO", 
      "message" : "[ ] - Upload do arquivo para o S3.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.107Z"), 
      "fileName" : "lab/PEOPLE/ID-46647-1493064966032-0-1-1493065242667.txt.gz" 
     }, 
     { 
      "status" : "INFO", 
      "message" : "[X] - Upload do arquivo para o S3.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.114Z") 
     }, 
     { 
      "status" : "INFO", 
      "message" : "[ ] - Transformando arquivo em pessoas.", 
      "informationDate" : ISODate("2017-04-24T20:32:23.121Z") 
     } 
    ] 
} 

そして、私は、次のクエリを実行しています:

db.taskExecution.aggregate([{ 
     $match: { 
      name: 'BTG-IMPORT-PEOPLE-TO-YOUKNOW', 
      executionDate: { 
       '$gte': ISODate("2017-05-03T00:00:00.000Z"), 
       '$lte': ISODate("2017-05-03T23:59:59.999Z") 
      } 
     } 
    }, 

    { 
     $project: { 
      NAME: "$name", 
      BEG: { $arrayElemAt : [ "$taskInformation.informationDate", 0 ]}, 
      START_DATE: { $arrayElemAt : [ "$taskInformation.informationDate", -1 ]}, 
      DURATION: { $divide : [ { $subtract: [ {$arrayElemAt : [ "$taskInformation.informationDate", -1 ]},{$arrayElemAt : [ "$taskInformation.informationDate", 0 ]}]}, 60000]}, 
      ERRORS: "$taskInformation.message", 
      STATUS: {$cond: [{$eq : ["$taskInformation.status", "ERROR"]}, "ERROR", { $arrayElemAt : [ "$taskInformation.status", -1 ]}]} 
     } 
    } 
]) 

投影のERRORSフィールドは、taskInformation.statusが "ERROR"である要素のtaskInformation.messageのみを持っていなければなりません。 STATUSフィールドは、taskInformation.statusのいずれかが「ERROR」である場合は「ERROR」を返し、何も「ERROR」でない場合は最後のtaskInformation.statusを返します。私はこれらの2つの分野でこれらの行動を実行するのにいくつかの問題を抱えています。誰か助けてくれますか?

+0

あなたは[ '$のfilter'](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#exp._S_filter)を使用して、見たことがありますか? – JohnnyHK

答えて

0

STATUSERRORについては、以下の予測を使用できます。

STATUS突起がtaskInformationstatusを確認する$map$anyElementTrueを使用し、真の状態が他ERROR最後の要素status

STATUS: { 
    $cond: [{ 
     $anyElementTrue: { 
      $map: { 
       input: "$taskInformation", 
       as: "resultm", 
       in: { 
        $eq: ["$$resultm.status", "ERROR"] 
       } 
      } 
     } 
    }, "ERROR", { 
     $arrayElemAt: ["$taskInformation.status", -1] 
    }] 
} 

STATUS突起がERRORステータスとディスプレイとtaskInformationフィルタリングする$map$filterを使用している場合message

ERRORS: { 
    $map: { 
     input: { 
      $filter: { 
       input: "$taskInformation", 
       as: "resultf", 
       cond: { 
        $eq: ["$$resultf.status", "ERROR"] 
       } 
      } 
     }, 
     as: "resultm", 
     in: "$$resultm.message" 
    } 
} 
関連する問題