Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongoose:如何使用聚合和展平子文档进行列表_Node.js_Mongodb_Mongoose_Mongodb Query_Aggregation Framework - Fatal编程技术网

Node.js Mongoose:如何使用聚合和展平子文档进行列表

Node.js Mongoose:如何使用聚合和展平子文档进行列表,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,在这种情况下,我怎样才能得到这个结果 我有一个叫硬币的收藏 [ { "_id" : ObjectId("5dc8c47f638267be1b00e808"), "mintTxid" : "abc371bb13034ed6acf96a39e09b22347f0038002eb8a21493032885ba6b77da", "address" : "mokZmpYj3vSqghQaZXZ8AGt1oo1HyidLow", "spentTxid" : "fddc7f

在这种情况下,我怎样才能得到这个结果

我有一个叫硬币的收藏

[
  {
    "_id" : ObjectId("5dc8c47f638267be1b00e808"),
    "mintTxid" : "abc371bb13034ed6acf96a39e09b22347f0038002eb8a21493032885ba6b77da",
    "address" : "mokZmpYj3vSqghQaZXZ8AGt1oo1HyidLow",
    "spentTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46"
  },
  {
    "_id" : ObjectId("5dc91d0d638267be1b21c2eb"),
    "mintTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46",
    "address" : "mwE7bR8nLF9G1jUY17DzRhdWRrs4fGppvA"
  }
]
我使用$lookup并加入了它的selfspenttxid=mintTxid

db.getCollection('coins').aggregate([
    { $match: {'address': 'mokZmpYj3vSqghQaZXZ8AGt1oo1HyidLow'}},
    {
        $lookup: {
            from: 'coins',
            localField: 'spentTxid',
            foreignField: 'mintTxid',
            as: 'spents'
        }
    },
    {
        $unwind: {
            path: '$spents',
            preserveNullAndEmptyArrays: true
        }
    }
])
这是一个结果

{
    "_id" : ObjectId("5dc8c47f638267be1b00e808"),
    "mintTxid" : "abc371bb13034ed6acf96a39e09b22347f0038002eb8a21493032885ba6b77da",
    "address" : "mokZmpYj3vSqghQaZXZ8AGt1oo1HyidLow",
    "spentTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46",
    "spents" : {
        "_id" : ObjectId("5dc91d0d638267be1b21c2eb"),
        "mintTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46",
        "address" : "mwE7bR8nLF9G1jUY17DzRhdWRrs4fGppvA",
    }
}
我怎样才能得到这样的结果?我使用了$replaceRoot选项,但该选项只返回子级

[
  {
    "_id" : ObjectId("5dc8c47f638267be1b00e808"),
    "mintTxid" : "abc371bb13034ed6acf96a39e09b22347f0038002eb8a21493032885ba6b77da",
    "address" : "mokZmpYj3vSqghQaZXZ8AGt1oo1HyidLow",
    "spentTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46",
  },
  {
    "_id" : ObjectId("5dc91d0d638267be1b21c2eb"),
    "mintTxid" : "fddc7f7c6492e0cf670ff4f96e7aaaeeee3d75c51538a35286b66b6707260b46",
    "address" : "mwE7bR8nLF9G1jUY17DzRhdWRrs4fGppvA",
  }
]

请帮助我…

在聚合后添加以下管道阶段,然后尝试:

  {
    $project: {
      array: {
        $concatArrays: [
          [
            {
              _id: "$$ROOT._id",
              address: "$$ROOT.address",
              mintTxid: "$$ROOT.mintTxid",
              spentTxid: "$$ROOT.spentTxid",

            }
          ],
          [
            "$$ROOT.spents"
          ]
        ]
      }
    }
  },
  {
    $unwind: "$array"
  },
  {
    $replaceRoot: {
      newRoot: "$array"
    }
  }
在这个项目中,我们创建了一个新的阵列,在其中我们根据需要推送两个阵列 展开阵列 用根目录中的数据替换根目录