How does mongoose handle a multi-tier circular query, modify the query results, and then return the final results?

problem description

there is a shopping cart cartList field under the user table. The field type is as follows:

cartList: [
    {
        shopId: {
            type: Schema.ObjectId
        },
        shopName: String,
        goodsList: [
            {
                _id: Schema.ObjectId,
                name: String,
                num: Number,
                selectedAttr: Array
            }
        ]
    }
]

then make a supplement to the price before taking this field. Here will involve two loops, one is to traverse the elements in the cartList, and the second is to traverse the goodsList array in the elements, and use the _ id inside to find the price in the goods table and add it. So the question is, because find queries are asynchronous, how can I get the correct results at the end of the loop?


mongoose find has a callback, or promise . If you have found the result, just implement the business logic in the callback.
reference

Menu