如何在Mongoose中将嵌入式文档列表作为平面数组获取?

时间:2016-01-20 19:42:45

标签: node.js mongodb mongoose

我有这个型号:

"client": {
    "title": { "type": "String", "required": true },
    "firstname": { "type": "String", "required": true },
    "lastname": { "type": "String", "required": true },
    "email": { "type": "String", "required": true, "index": { "unique": true } ...
    "purchases": [{ "amount": "Number", "productID": "String", "transactionDate": "Date"}],
    "complementaryNote": "String"
}

我想这样购买:

[{ "amount": "Number", "productID": "String", "transactionDate": "Date"}, { "amount": "Number", "productID": "String", "transactionDate": "Date"} ...]

我使用此查询:

Client.find(pQuery).sort('-purchases.transactionDate').select('purchases').exec(function(err, pResults) {
            pResultCallback(pResults)})

这给出了这个结果:

[
    {
        "_id": "569e50cf4895ced01aa55d9c",
        "purchases": [
            {
                "amount": 88761,
                "transactionDate": "2016-01-19T15:02:37.800Z",
                "productID": "Awesome Fresh Tuna",
                "_id": "569e50cf4895ced01aa55d9d"
            }
        ]
    },
    {
        "_id": "569e50df4895ced01aa589ae",
        "purchases": [
            {
                "amount": 66487,
                "transactionDate": "2016-01-19T14:02:19.777Z",
                "productID": "Licensed Cotton Keyboard",
                "_id": "569e50df4895ced01aa589af"
            }
        ]} ... ]

我想要的是:

[{
                    "amount": 88761,
                    "transactionDate": "2016-01-19T15:02:37.800Z",
                    "productID": "Awesome Fresh Tuna",
                    "_id": "569e50cf4895ced01aa55d9d"
                },
{
                    "amount": 66487,
                    "transactionDate": "2016-01-19T14:02:19.777Z",
                    "productID": "Licensed Cotton Keyboard",
                    "_id": "569e50df4895ced01aa589af"
                } ... ]

所以我最终可以在交易日期订购它们。

感谢有人试图提供帮助。

0 个答案:

没有答案