Есть 2 коллекции - продукты, пользователи, которым я хочу получить корзину от пользователей, и коллекция продуктов на основе productID в mongodb
users
{"_id":{"$oid":"5eccb6c40bd7691618efac51"},"username":"Penny","emailId":"penny@gmail.com","password":"6ab907b0e30ed54f80ad41e602cf7dde:e3393d14abb7ad9b51c9038e82577a22","contact":1000,"cart":{"cartItems":[{"prod_id":"5ecb827af14d2e213c0b6e4c","prod_qty":3},{"prod_id":"5ecb837ff14d2e213c0b6e4f","prod_qty":1},{"prod_id":"5ecb8309f14d2e213c0b6e4e","prod_qty":2},{"prod_id":{"$oid":"5ecb82cdf14d2e213c0b6e4d"},"prod_qty":3}],"cartTotalPrice":7900}}
продукты:
{"_id":{"$oid":"5ecb827af14d2e213c0b6e4c"},"title":"Lord Ganesha Idol","imageUrl":"https://www.thestatesman.com/wp-content/uploads/2019/08/ganesh.jpg","description":"Auspicious Idol of Lord Ganesh","price":"1000","totalQty":100,"seller":"rio@gmail.com"}
{"_id":{"$oid":"5ecb82cdf14d2e213c0b6e4d"},"title":"Chocolate cake","imageUrl":"https://i.pinimg.com/originals/1c/0e/1d/1c0e1dc6580a002af045de8fc095d521.jpg","description":"Chocolate cake","price":"800","totalQty":80,"seller":"rio@gmail.com"}
{"_id":{"$oid":"5ecb8309f14d2e213c0b6e4e"},"title":"Cake","imageUrl":"https://tatyanaseverydayfood.com/wp-content/uploads/2018/07/Summer-Sangria-Cake-4.jpg","description":"Cake with fruit toppings","price":"900","totalQty":"90","seller":"rio@gmail.com"}
{"_id":{"$oid":"5ecb837ff14d2e213c0b6e4f"},"title":"Lord Ram","imageUrl":"https://i.pinimg.com/originals/99/df/bd/99dfbd30f455e38fc7f4a326ab3d1c24.jpg","description":"Photo frame","price":"700","totalQty":60,"seller":"denver@gmail.com"}
{"_id":{"$oid":"5ecb83b8f14d2e213c0b6e50"},"title":"Chocolates","imageUrl":"https://4.imimg.com/data4/FN/FU/MY-11336933/sweet-chocolates-500x500.jpg","description":"Belgium Chocs","price":"800","totalQty":40,"seller":"denver@gmail.com"}
Рабочий код: - Нужен лучший (может быть с объединениями) getCart ()
{
const db=getDb();
const productIds = this.cart.cartItems.map(i => {
return i.prod_id;
});
//console.log("prodids",productIds)
return db
.collection('products')
.find({ _id: { $in: productIds } })
.toArray()
.then(products => {
return products.map(p => {
return {
...p,
quantity: this.cart.cartItems.find(i => {
return i.prod_id.toString() === p._id.toString();
}).prod_qty,
cartTotalPrice:this.cart.cartTotalPrice
};
});
});
}
ВЫХОД: Товары в корзине [{_id: 5ecb827af14d2e213c0b6e4 c, title: 'Lord Ganesha Idol', imageUrl: 'https://www.thestatesman.com/wp-content/uploads/2019/08/ganesh.jpg', description: 'Благоприятный идол лорда Гейна sh ', цена:' 1000 ', totalQty: 50, продавец:' rio@gmail.com ', количество: 6, cartTotalPrice: 15000}, {_id: 5ecb82cdf14d2e213c0b6e4d, title:' Chocolate cake ', imageUrl:' https://i.pinimg.com/originals/1c/0e/1d/1c0e1dc6580a002af045de8fc095d521.jpg ', описание:' Шоколадный торт ', цена:' 800 ', всего: 20, продавец:' rio@gmail.com ', количество: 8, cartTotalPrice: 15000}, {_id: 5ecb837ff14d2e213c0b6e4f, title:' Господь Ram ', imageUrl:' https://i.pinimg.com/originals/99/df/bd/99dfbd30f455e38fc7f4a326ab3d1c24.jpg ', описание:' Photo frame ', цена:' 700 ', totalQty: 7, продавец:' denver@gmail.com ', количество: 1, cartTotalPrice: 15000} , {_id: 5ecb83b8f14d2e213c0b6e50, title: 'Chocolates', imageUrl: 'https://4.imimg.com/data4/FN/FU/MY-11336933/sweet-chocolates-500x500.jpg', описание: 'Belgium Chocs', цена: '800', totalQty: 35, Seller: 'denver@gmail.com', количество: 1, cartTotalPrice: 15000}]