I’m trying to get a specific result of my database in mongoose, but this is what i have its working but the code looks pretty bad.
const completedOrders = await Order.find({ "status": "delivered" }).
populate('address');
the response of the database is this and i trying to get all the states of every order with status: “delivered”
"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"
}
]
so this is what i tried but its quite dirty and i still want to do the same for city, and provinces, is for the analitycs of my ecommerce
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;
}));