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

  node.js, question

Problem description

The cartList field below the user table has the following field types:

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

Then, before taking this field, add a price. There will be two goods involved, one is to traverse the elements in cartList, the second is to traverse the GoodList array in the elements, and find the price in the GoodList and add it through the _id inside. Then the problem arises, because the find query is asynchronous, how can I get the correct result after the loop ends?

mongooseThefindThere is a callback, orpromiseWriting style. After finding the result, it is ok to implement the business logic in the callback to inside.
Reference document