【问题标题】:Return the result of an aggregate as an object not an array将聚合的结果作为对象而不是数组返回
【发布时间】:2020-08-04 20:07:36
【问题描述】:

我的收藏中有这样的模型..

{
    "_id": {
        "$oid": "5f213786d0cdd10c61969fc7"
    },
    "stationStatus": "Online",
    "metadata": {
        "SerialNumber": "BP001",
        "imsi": "082943327103528941"
    },
    "boxIdentity": "BP001",
    "__v": 0
}

{
    "_id": {
        "$oid": "5f213786d0cdd10c61969fc7"
    },
    "stationStatus": "Offline",
    "metadata": {
        "SerialNumber": "BP002",
        "imsi": "082943327103528941"
    },
    "boxIdentity": "BP002",
    "__v": 0
}

这是我对数据执行的聚合。

       var aggregation = [
        {
          $project: { __v: false },
        },
        {
          $group: {
            _id: { $toLower: "$stationStatus" },
            stations: {
              $push: "$$ROOT",
            },
          },
        },
        {
          $group: {
            _id: null,
            stations: {
              $push: {
                k: "$_id",
                v: "$stations",
              },
            },
          },
        },
        {
          $replaceRoot: {
            newRoot: { $arrayToObject: "$stations" },
          },
        },
      ];
      Model.aggregate(aggregation)
        .then(function (res) {
          console.log(res);
          resolve(res);
        })
        .catch(function (err) {
          reject(err);
        });

我在数组中接收到的数据。我需要它的数据只返回对象中管道的结果,而不是最顶层数组中的对象。如何以某种方式返回结果,以便只返回数组的第一个对象,因为聚合将返回的只是一个对象。

 [
        {
            "online": [
                {
                    "_id": "5f213786d0cdd10c61969fc7",
                    "stationStatus": "Online",
                    "connectors": [
                        {
                            "_id": "5f213786d0cdd10c61969fc8",
                           ...
                        },
                        {
                            "_id": "5f213791d0cdd10c61969fcf",
                            ...
                        },
                        {
                            "_id": "5f213791d0cdd10c61969fd1",
                            ...
                        }
                    ],
                    "metadata": {
                        "SerialNumber": "BP001",
                        "imsi": "082943327103528941"
                    },
                    "boxIdentity": "BP001"
                },
                {
                    "_id": "5f2809d7b03827069d3b5627",
                    "stationStatus": "Online",
                    "connectors": [
                        {
                            "_id": "5f213786d0ccc310c61969fc8",
                           ...
                        },
                        {
                            "_id": "5f213791d0cd340c61969fcf",
                            ...
                        },
                        {
                            "_id": "5f213791d0cdd10c61369fd1",
                            ...
                        }
                    ],
                    "metadata": {
                        "SerialNumber": "BP002",
                        "imsi": "0963433223503528941"
                    },
                    "boxIdentity": "BP002"
                }
            ]
        }
    ]

【问题讨论】:

    标签: mongodb express mongoose aggregation-framework


    【解决方案1】:

    聚合总是返回一个数组。

    如果你使用 es6,你可以解构这个值。

    Model.aggregate(aggregation)
      .then(function ([theObjectYouWant]) {
        console.log(theObjectYouWant);
        ....
      
    
    

    【讨论】:

    • 我正在使用 ES6。如何解构以获得第一个元素?
    • 仔细看看我的答案中的sn-p,回调正在破坏结果以获取theObjectYouWant
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多