最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何使用Mongoose在MongoDB中将数组的聚合输出转换为对象?

IT培训 admin 4浏览 0评论

如何使用Mongoose在MongoDB中将数组的聚合输出转换为对象?

我在使用Mongoose的MongoDB聚合中遇到问题。在mongo控制台中,我将输出作为单个对象输出,但是Mongoose返回了一个数组。假设我们有一个users文档,其中包含以下数据:

{
  "id": "1",
  "relative": ["1", "2", "3"]
}

和具有以下数据的relatives文档:

{
  "id": "1",
  "name": "A",
  "imageUrl": "abc" 
},
{
  "id": "2",
  "name": "B",
  "imageUrl": "pqr" 
},
{
  "id": "3",
  "name": "C",
  "imageUrl": "xyz" 
}

我的目标是对上述文档进行分页(使用跳过和限制),并实现到以下步骤:

db.users.aggregate([
  { $match: { "id": "1" } },
  { $addFields: { listSize: { $size: `$relative` } } },
  { $unwind: `$relative` },
  { $lookup: { 
     "from": "relatives", 
     "localField": "relative", 
     "foreignField": "id",
     "as": "list"
  }},
  { $addFields: { [`list.listSize`]: "$listSize" } },
  { $unwind: `$list` },
  { $skip: 1 },
  { $addFields:
    { "isMore": { 
        $cond: { 
          if: { $gt: ["$listSize", 1] }, 
          then: "true", else: "false"
        }
      },
      "index": 0 + 1
    }
  },
  { $limit: 1 },
  { $group: { 
      _id: { isMore: "$isMore", index: "$index" },
      list: { $push: `$list` }
    }
  },
  { $project:
    { _id: 0, 
      index: "$_id.index", 
      isMore: "$_id.isMore",
      "list": 1
    }
  }
]).pretty();

这里:

  • 1是要跳过和限制的文档数。
  • 0是当前页码。

在mongo控制台中,以下聚合的输出是:

{
    "list" : [
        {
            "_id" : ObjectId("..."),
            "id" : "2",
            "name" : "B",
            "imageUrl" : "pqr",
            "listSize" : 3
        }
    ],
    "index" : 1,
    "isMore" : "true"
}

但是在猫鼬中,我得到一个数组作为带有一个如下元素的输出:

[
  {
    "list" : [
        {
            "_id" : ObjectId("..."),
            "id" : "2",
            "name" : "B",
            "imageUrl" : "pqr",
            "listSize" : 3
        }
    ],
    "index" : 1,
    "isMore" : "true"
  }
]

我有此问题的解决方案,即在Java脚本端转换数组,但我想在MongoDB端进行转换。

是否有解决此问题的方法?

回答如下:

正如此post所说,它返回数组是Aggregate行为的一部分。如果只需要第一个元素,只需获取第一个元素

如何使用Mongoose在MongoDB中将数组的聚合输出转换为对象?

我在使用Mongoose的MongoDB聚合中遇到问题。在mongo控制台中,我将输出作为单个对象输出,但是Mongoose返回了一个数组。假设我们有一个users文档,其中包含以下数据:

{
  "id": "1",
  "relative": ["1", "2", "3"]
}

和具有以下数据的relatives文档:

{
  "id": "1",
  "name": "A",
  "imageUrl": "abc" 
},
{
  "id": "2",
  "name": "B",
  "imageUrl": "pqr" 
},
{
  "id": "3",
  "name": "C",
  "imageUrl": "xyz" 
}

我的目标是对上述文档进行分页(使用跳过和限制),并实现到以下步骤:

db.users.aggregate([
  { $match: { "id": "1" } },
  { $addFields: { listSize: { $size: `$relative` } } },
  { $unwind: `$relative` },
  { $lookup: { 
     "from": "relatives", 
     "localField": "relative", 
     "foreignField": "id",
     "as": "list"
  }},
  { $addFields: { [`list.listSize`]: "$listSize" } },
  { $unwind: `$list` },
  { $skip: 1 },
  { $addFields:
    { "isMore": { 
        $cond: { 
          if: { $gt: ["$listSize", 1] }, 
          then: "true", else: "false"
        }
      },
      "index": 0 + 1
    }
  },
  { $limit: 1 },
  { $group: { 
      _id: { isMore: "$isMore", index: "$index" },
      list: { $push: `$list` }
    }
  },
  { $project:
    { _id: 0, 
      index: "$_id.index", 
      isMore: "$_id.isMore",
      "list": 1
    }
  }
]).pretty();

这里:

  • 1是要跳过和限制的文档数。
  • 0是当前页码。

在mongo控制台中,以下聚合的输出是:

{
    "list" : [
        {
            "_id" : ObjectId("..."),
            "id" : "2",
            "name" : "B",
            "imageUrl" : "pqr",
            "listSize" : 3
        }
    ],
    "index" : 1,
    "isMore" : "true"
}

但是在猫鼬中,我得到一个数组作为带有一个如下元素的输出:

[
  {
    "list" : [
        {
            "_id" : ObjectId("..."),
            "id" : "2",
            "name" : "B",
            "imageUrl" : "pqr",
            "listSize" : 3
        }
    ],
    "index" : 1,
    "isMore" : "true"
  }
]

我有此问题的解决方案,即在Java脚本端转换数组,但我想在MongoDB端进行转换。

是否有解决此问题的方法?

回答如下:

正如此post所说,它返回数组是Aggregate行为的一部分。如果只需要第一个元素,只需获取第一个元素

发布评论

评论列表 (0)

  1. 暂无评论