如何提取单个数组中的所有金额?

时间:2018-09-25 12:47:54

标签: node.js mongodb

 [
{
    "timestamp": "2018-09-25T12:38:10.436Z",
    "_id": "5ba1154d7c0f70472c6cd97d",
    "rate": [
        {
            "_id": "5ba116f6ffcbd8369811653a",
            "amount": 5004,
            "makerfee": 0.1,
            "takerfee": 0.2,
            "cc_exchange": "Bitfinex"
        },
        {
            "_id": "5baa0dd1168c0e448469c0b0",
            "amount": 100000,
            "makerfee": 0.2,
            "takerfee": 0.3,
            "cc_exchange": "Bitfinex"
        },
        {
            "_id": "5baa1e1c5e3bfc4554e72225",
            "amount": 150000,
            "makerfee": 0.3,
            "takerfee": 0.4,
            "cc_exchange": "Bitfinex"
        }
    ],
    "cc_exchange": "Bitfinex",
    "user": "5ba10e5f7d1a6e0a40b82812",
    "__v": 1
},
{
    "timestamp": "2018-09-25T12:38:10.443Z",
    "_id": "5ba381cef579772bc48f241a",
    "rate": [
        {
            "_id": "5ba381cef579772bc48f241b",
            "amount": 5,
            "makerfee": 0.1,
            "takerfee": 0.2,
            "cc_exchange": "Cex"
        },
        {
            "_id": "5baa1d7a9bbfb415a43f7730",
            "amount": 10,
            "makerfee": 0.2,
            "takerfee": 0.3,
            "cc_exchange": "Cex"
        },
        {
            "_id": "5baa1df75e3bfc4554e72224",
            "amount": 15,
            "makerfee": 0.3,
            "takerfee": 0.4,
            "cc_exchange": "Cex"
        }
    ],
    "cc_exchange": "Cex",
    "user": "5ba10e5f7d1a6e0a40b82812",
    "__v": 0
}]

这是mongo文件。我只想提取单个数组中的金额。我在后端使用节点。请告诉我该怎么做。我尝试使用 for 循环,但无法提取它。请让我知道它甚至可能与查询。预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以在node.js端使用Javascript提取金额,如下所示:

const document = [ /* your document */ ];
const amounts = [];
document.map(elem => elem.rate)
        .map(rateArr => rateArr.forEach(rate => amounts.push(rate.amount)));

这将创建一个像这样的数组:

[5004, 100000, 150000, 5, 10, 15]