我的代码有点脏,我想不出改进它的方法,我该怎么做才能得到更紧凑、更好的解决方案?

i have a code that its kinda dirty and i cant think of a way to improve it, what can i do to have a more compact and better solution?

我试图在 mongoose 中获取我的数据库的特定结果,但这是我的工作,但代码看起来很糟糕。

 const completedOrders = await Order.find({ "status": "delivered" }).
    populate('address');

数据库的响应是这样的,我试图获取每个订单的所有状态,状态为:“已交付”

"completedOrders": [
    {
        "_id": "61b81eef631dcc413e98515a",
        "user": "61b51dcbfd50717dc6dc931a",
        "address": {
            "address": {
                "phone": 54156345645768,
                "rut": "26.232.966-6",
                "state": "metropolitana",
                "city": "dunno",
                "province": "pepe",
                "street": "asdaqqqqqqqda",
                "numstreet": 999
            },
            "_id": "61b8179e7dfc15f26c561175",
            "user": "61b51dcbfd50717dc6dc931a",
            "__v": 0
        },
        "status": "delivered",
        "orderItems": [
            "61b81ee6631dcc413e985146",
            "61b81ee6631dcc413e985147"
        ],
        "shipping": 3500,
        "code": "c986aeb3-1fc9-422e-8638-b40651d7906c",
        "total": 690,
        "totalCost": 370,
        "createdAt": "2021-12-14T04:34:55.564Z",
        "updatedAt": "2021-12-14T04:34:55.564Z"
    },
    {
        "_id": "61b81fddda2eb87de7d44c42",
        "user": "61b51dcbfd50717dc6dc931a",
        "address": {
            "address": {
                "phone": 54156345645768,
                "rut": "26.232.966-6",
                "state": "metropolitana",
                "city": "dunno",
                "province": "pepe",
                "street": "asdaqqqqqqqda",
                "numstreet": 999
            },
            "_id": "61b8179e7dfc15f26c561175",
            "user": "61b51dcbfd50717dc6dc931a",
            "__v": 0
        },
        "status": "delivered",
        "orderItems": [
            "61b81fdbda2eb87de7d44c32",
            "61b81fdbda2eb87de7d44c33"
        ],
        "shipping": 3500,
        "code": "e2828a65-ea12-43e9-9909-f081c9cd32e9",
        "total": 690,
        "totalCost": 370,
        "createdAt": "2021-12-14T04:38:53.517Z",
        "updatedAt": "2021-12-14T04:38:53.517Z"
    }
]

所以这是我尝试过的方法,但它很脏,我仍然想对城市和省份做同样的事情,这是为了我的电子商务分析

    const AddressesUser = await Promise.all(completedOrders.map(async (array) => {
        const allAddresses = array.address;
        return allAddresses;
    }));

    const arrayAddresses = await Promise.all(AddressesUser.map(async (array) => {
        const allAddresses = array.address;
        return allAddresses;
    }));

    
    const allStates = await Promise.all(arrayAddresses.map(async (array) => {
        const states = array.state;
        return states;
    }));

如果你想要顶层的状态字段,你可以使用聚合:

[{"$match":{"status":"delivered"}},{"$addFields":{"state":"$address.address.state"}},{"$project":{"state":1,"status":1}}]

如果您需要任何其他参数,您只需在项目阶段添加即可。

如果你只想要状态键,这个聚合流程也会有帮助:

[{"$match":{"status":"delivered"}},{"$replaceRoot":{"newRoot":{"state":"$address.address.state"}}}]

这将替换根,并且将删除所有高于该级别的密钥。