を私は下図のよう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つの分野でこれらの行動を実行するのにいくつかの問題を抱えています。誰か助けてくれますか?
あなたは[ '$のfilter'](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#exp._S_filter)を使用して、見たことがありますか? – JohnnyHK