【问题标题】:Mongoose aggregate result in custom formatMongoose 以自定义格式聚合结果
【发布时间】:2018-08-01 17:26:58
【问题描述】:

我正在将 mongodb 与 mongoose 一起使用,并尝试在我的回调中重现这种格式,如下所示:

{
warning:[ {
    "_id": "5a8dcdbcadb65b484e888091",
    "application_name": "teste",
    "created_at": "2018-02-02T23:55:06.000Z",
    "log_identifier": "1"
  }, {
    "_id": "5a8dcdbcadb65b484e888091",
    "application_name": "teste",
    "created_at": "2018-02-02T23:55:06.000Z",
    "log_identifier": "1"
  }],
error:[ {
    "_id": "5a8dcdbcadb65b484e888091",
    "application_name": "teste",
    "created_at": "2018-02-02T23:55:06.000Z",
    "log_identifier": "1"
  }, {
    "_id": "5a8dcdbcadb65b484e888091",
    "application_name": "teste",
    "created_at": "2018-02-02T23:55:06.000Z",
    "log_identifier": "1"
  }]
}

这是我的模型:

var logSchema = new Schema({
    application_name: String,
    file_name: String,
    type: String,
    created_at: Date,
    log_identifier: String,
    environment: String
 });

我正在使用聚合方法按类型设置类型和分组,如下所示:

LogModel.aggregate([
        {
            "$unwind": "$type"
        }, {

            $group: {
                "_id": {
                    type: "$type"
                },

                "Warning": {

                    "$push": {
                        "$cond": {
                            if: {
                                $eq: ["$type", "Warning"]
                            },
                            then: {
                                "_id": "$_id",
                                "application_name": "$application_name",
                                "created_at": "$created_at",
                                "log_identifier": "$log_identifier",
                                "enviroment": "$enviroment"
                            },
                            else: false,
                        }
                    }
                },
                "Error": {

                    "$push": {
                        "$cond": {
                            if: {
                                $eq: ["$type", "Error"]
                            },
                            then: {
                                "_id": "$_id",
                                "application_name": "$application_name",
                                "created_at": "$created_at",
                                "log_identifier": "$log_identifier",
                                "enviroment": "$enviroment"
                            },
                            else: false,
                        }
                    }
                }
            }
        }


    ], function (err, logs) {
        if (err) reject(err);
        else resolve(logs)
    });

但我的结果是

[
  {
    "_id": {
     "type": "Error"
    },
    "Warning": [
  false
],
 "Error": [
   {
     "_id": "5a8dd312c681334d558e6169",
     "application_name": "teste",
     "created_at": "2018-02-02T23:55:06.000Z",
     "log_identifier": "1"
   }
 ]
  },
  {
    "_id": {
      "type": "Warning"
    },
    "Warning": [
      {
        "_id": "5a7b6a36a5799090cf76bc4c",
        "application_name": "teste",
        "created_at": "2018-02-02T23:37:06.000Z",
        "log_identifier": "1"
      },
      {
        "_id": "5a7b6a49a5799090cf76bc4d",
        "application_name": "teste",
        "created_at": "2018-02-02T23:55:06.000Z",
        "log_identifier": "1"
      },
      {
        "_id": "5a8dcdbcadb65b484e888091",
        "application_name": "teste",
        "created_at": "2018-02-02T23:55:06.000Z",
        "log_identifier": "1"
      }
    ],
    "Error": [
      false,
      false,
      false
    ]
  }
]

我不想要具有错误值的字段,并且我不知道其他方法可以重现我想要的内容。

谁能帮帮我?

【问题讨论】:

    标签: javascript node.js mongodb mongoose aggregation-framework


    【解决方案1】:

    你可以在 3.4 版本中重写你的聚合。

    $group 按类型并推送每种类型的值。 $replaceRoot$arrayToObject 输出命名键。

    LogModel.aggregate([
      {"$group":{
        "_id":"$type",
        "data":{
          "$push":{
            "_id":"$_id",
            "application_name":"$application_name",
            "created_at":"$created_at",
            "log_identifier":"$log_identifier",
            "enviroment":"$enviroment"
          }
        }
      }},
      {"$replaceRoot":{
        "newRoot":{
          "$let":{
            "vars":{"array":[["$_id","$data"]]},
            "in":{"$arrayToObject":"$$array"}}
        }
      }}
    ])  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2020-02-01
      • 2014-02-19
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      相关资源
      最近更新 更多