This is the Mongo db collection
{
"_id": "62035cd8a76fcc0e09f46c22",
"particulars":[
{
"serialNo": 1,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
},
{
"serialNo": 2,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
},
{
"serialNo": 3,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
}
]
}
the array from frontend is this
procurement_details:[
{
"serialNo": 1,
"item": "ac",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 2,
"item": "fan",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
}
]
output i want . Need to replace the matching serialNo objects in particular with the objects in particulars
particulars:[
{
"serialNo": 1,
"item": "ac",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 2,
"item": "fan",
"link": "http//",
"images": "http//",
"quantity": 1,
"unitPrice": 100,
"amount": 100
},
{
"serialNo": 3,
"item": "desk",
"link": "http//",
"images": "http//",
"quantity": 13,
"unitPrice": 100,
"amount": 1300
}
]
i need to replace the objects in particulars with the new objects from procurement_details with respect to the serialNo.so can anyone help me to figure this out and im using nodejs and mongoClient.
var bulk=await tickets.initializeOrderedBulkOp()
for(procurement_detail of procurement_details){
const resultt=await tickets.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo},{projection:{"particulars.$":1,_id:0}}).toArray()
console.log("resultts",resultt);
await bulk.find({"_id":mongodb.ObjectId(ticketId),"particulars.serialNo":procurement_detail.serialNo}).update({
$set:{"particulars.$":procurement_detail},
$push:{ticketHistory:{$each:resultt[0].particulars}}
})
}
await bulk.execute()
Related
Whenever a customer checks out its saves an order that has products ordered by a customer and the product id, quantity and customers details like delivery address and customer phone number.
I want the same details to be added to all products in that order also so that the seller can also see that his product has been ordered
This i can achieve using each product's id in the orders since every product has its ID . But i dont know how to map the order's products id's and find the products in the products schema then add to them an array called orders.
This is how my orders schema looks like after an order has been placed
{
"allOrders": [
{
"_id": "6249ee9baa59aceb7d88f91d",
"placedby": "624471321dff6794a802431c",
"fullname": "Collins Jimu",
"deliveryaddress": "cnr fourth and herbet chitepo",
"phonenumber": "0773468496",
"whatsappphonenumber": "0773468496",
"ordersList": [
{
"name": "Hp 200 G4Core I5",
"qty": 1,
"price": 560,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648734312/cwynmzauqqnylrwsgejy.jpg",
"id": "6245b0c09390975e16263136"
},
{
"name": "bed",
"qty": 1,
"price": 160,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732217/dtrb8saj0ellgyrcjpyy.jpg",
"id": "6245a88cf22df558d8d69162"
},
{
"name": "Sunglass",
"qty": 1,
"price": 6,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648738341/rtpiptqc1le6kywmuisf.jpg",
"id": "6245c03bf22df558d8d6a739"
},
{
"name": "sofas",
"qty": 1,
"price": 890,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648732837/xae655b5e9wovodfkvqn.jpg",
"id": "6245aacdf22df558d8d6939f"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T18:59:39.681Z",
"updatedAt": "2022-04-03T18:59:39.681Z",
"__v": 0
},
{
"_id": "6249fdc602cefac4faa36dde",
"placedby": "624471321dff6794a802431c",
"fullname": "My MUM",
"deliveryaddress": "123 HARARE STREET",
"phonenumber": "0773550804",
"whatsappphonenumber": "0773550804",
"ordersList": [
{
"name": "out door benches",
"qty": 1,
"price": 25,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648736078/in8rumwcm6rrebrmgb1q.jpg",
"id": "6245b792be41db13f9220867"
},
{
"name": "hammer",
"qty": 1,
"price": 10,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648735570/joceetdkx4penomtab8s.jpg",
"id": "6245b5849390975e16263647"
},
{
"name": "Watches",
"qty": 1,
"price": 4,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648735962/r4jmwlj7ltgc0njdtfdc.jpg",
"id": "6245b71abe41db13f92207ac"
},
{
"name": "iPhone X",
"qty": 1,
"price": 350,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648729600/ekcb86lmtkr0uniirb2i.jpg",
"id": "62459fc3be41db13f921f27a"
},
{
"name": "REFRIDGERATOR",
"qty": 1,
"price": 370,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648731962/fjrlvfnotdbonqnt9pjn.jpg",
"id": "6245a750be41db13f921f796"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T20:04:22.349Z",
"updatedAt": "2022-04-03T20:04:22.349Z",
"__v": 0
},
{
"_id": "624a12ea02cefac4faa431fd",
"placedby": "624471321dff6794a802431c",
"fullname": "Munyaradzi Makosa",
"deliveryaddress": "cnr fourth and herbet",
"phonenumber": "0777727363663",
"whatsappphonenumber": "0777727363663",
"ordersList": [
{
"name": "Wheels",
"qty": 1,
"price": 35,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648741496/xs9zpht0rhxpijrzyqrz.jpg",
"id": "6245cc859390975e162657ce"
},
{
"name": "MacBook Pro 2021",
"qty": 1,
"price": 1250,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648731661/h0dcvvhwntdy06mzgpah.webp",
"id": "6245a6649390975e16262737"
},
{
"name": "electric kettle",
"qty": 1,
"price": 22,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648734446/tyrdlrayn3opm7gagwmi.jpg",
"id": "6245b1239390975e162631e3"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-03T21:34:34.721Z",
"updatedAt": "2022-04-03T21:34:34.721Z",
"__v": 0
},
{
"_id": "624aa7cd02cefac4faa4750c",
"placedby": "624aa7a102cefac4faa474f3",
"fullname": "Ryan Katayi",
"deliveryaddress": "4164 Gwabalanda",
"phonenumber": "0718518438",
"whatsappphonenumber": "0718518438",
"ordersList": [
{
"name": "Budi 2017",
"qty": 1,
"price": 250,
"imgUrl": "/assets/images/products/Automotive/2.Audi2017.png",
"id": "6270512109915791"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-04T08:09:49.618Z",
"updatedAt": "2022-04-04T08:09:49.618Z",
"__v": 0
},
{
"_id": "624c0e770d2cf94693988609",
"placedby": "624aa7a102cefac4faa474f3",
"fullname": "Ryan Katayi",
"deliveryaddress": "4164 Gwabalanda",
"phonenumber": "0718518438",
"whatsappphonenumber": "0778687452",
"ordersList": [
{
"name": "Nike Airforce",
"qty": 2,
"price": 40,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1649082553/hoodaley8bzrwa8xi2tr.jpg",
"id": "624b01250d2cf94693982652"
},
{
"name": "iPhone X",
"qty": 1,
"price": 350,
"imgUrl": "https://res.cloudinary.com/kwingy/image/upload/v1648729600/ekcb86lmtkr0uniirb2i.jpg",
"id": "62459fc3be41db13f921f27a"
}
],
"paymentmethod": "cashondeliver",
"orderstatus": "placed",
"createdAt": "2022-04-05T09:40:07.719Z",
"updatedAt": "2022-04-05T09:40:07.719Z",
"__v": 0
}
]
}
i have managed to get the ids of all the orders. But my challenge is i am stuck with only the products ids not sure how to use them and find each products and then patch them with an orders details array
const sellerProductsOrdered = asyncHandler (async (req, res) => {
const Orders = await Order.find();
const ordersList = Orders.map(order => {
return order
})
const ordersListProductsIds = ordersList.map(product => {
return product.id
})
console.log(ordersListProductsIds);
})
Currently this is an example of a dataset in my salesOrder collection
{
"totalCost": 4,
"salesOrderRef": "s4",
"customerRef": "c10",
"date": "2021-06-03T00:00:00.000Z",
"etd": "2021-06-24T00:00:00.000Z",
"delivered": true,
"paid": false,
"inventory": [{
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
}, {
"sku": "i2",
"quantity": 2,
"priceEa": 2,
"discount": 2
}]
}
How do I get it to match
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
}
if I search sku: i3? If there are any duplicate sku in the inventory array of a single salesOrder document, I would also like it to duplicate another result out, like
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 1,
"priceEa": 2,
"discount": "0"
},
{
"salesOrderRef": "s4",
"customerRef": "c10",
"sku": "i3",
"quantity": 3,
"priceEa": 4,
"discount": "0"
}
How could I achieve this? I tried using aggregation and filters but I'm not sure how
Try this aggregation query, from what I understood this is should give you the output document you have mentioned.
db.collection.aggregate([
{
$unwind: "$inventory"
}, {
$project: {
"_id" : 0,
"salesOrderRef": 1,
"customerRef": 1,
"sku": "$inventory.sku",
"quantity": "$inventory.quantity",
"priceEa": "$inventory.priceEa",
"discount": "$inventory.discount"
}
}, ])
I have a database of exercises in a workout tracker, and when I do a find(), the result is this:
[
{
"_id": "5e9dacbb6512969974bd5b2d",
"day": "2020-04-10T14:07:55.905Z",
"exercises": [
{
"type": "resistance",
"name": "Bicep Curl",
"duration": 20,
"weight": 100,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2e",
"day": "2020-04-11T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Lateral Pull",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b2f",
"day": "2020-04-12T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Push Press",
"duration": 25,
"weight": 185,
"reps": 8,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b30",
"day": "2020-04-13T14:07:55.916Z",
"exercises": [
{
"type": "cardio",
"name": "Running",
"duration": 25,
"distance": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b31",
"day": "2020-04-14T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 285,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b32",
"day": "2020-04-15T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b33",
"day": "2020-04-16T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Quad Press",
"duration": 30,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b34",
"day": "2020-04-17T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b35",
"day": "2020-04-18T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Military Press",
"duration": 20,
"weight": 300,
"reps": 10,
"sets": 4
}
]
},
{
"_id": "5e9dacbb6512969974bd5b36",
"day": "2020-04-19T14:07:55.916Z",
"exercises": [
{
"type": "resistance",
"name": "Bench",
"duration": 30,
"distance": 2
}
]
}
]
Then I need to get total sums of statistics from each exercise, so I used mongoose aggregate to give me this data:
[
{
"_id": null,
"totalDuration": 230,
"totalWeight": 2070,
"totalSets": 32,
"totalReps": 78,
"totalDistance": 6
}
]
I want to combine these two results in one GET request, ideally doing something similar to a push where I just push the totals at the end of the first JSON object. How do I achieve this?
Something like this:
function mergeResults(resultFromFindQuery, totalSums){
var allData = {};
allData['mongoFindresult'] = resultFromFindQuery;
allData['totalSums'] = totalSums;
return allData;
}
Then use the returned value to what you need to do. Now you have both of them in the same variable.
i have this two arrays of objects:
bookUnitIdInformacoes
[
{
"id": 5,
"book_id": 33,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 6,
"book_id": 33,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 7,
"book_id": 33,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
},
{
"id": 8,
"book_id": 39,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
and idioma:
[
{
"id": 13,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 10:04:47",
"updated_at": "2020-01-05 16:08:00",
"language_substring": "US"
},
{
"id": 14,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:06:44",
"updated_at": "2019-12-30 11:06:44",
"language_substring": null
}
]
I'm grouping the bookUnitIdInfomacoes by book_id reducing the array into an object of key => value pairs as book_id => array of books
Then, using [ Object.values ] to retrieve only the values of this groupedObj object.
Lastly, we map idiomas and add the corresponding book group into a new property called quizz . For a given idioma , we know its position in idiomas thanks to the second argument of map : i . We simply do grouped[i] to get the corresponding group of books.
The code:
const groupedObj = bookUnitIdInformacoes.reduce((grouped, info) => {
grouped[info.book_id] = grouped[info.book_id] || [];
grouped[info.book_id].push(info);
return grouped;
}, {});
const grouped = Object.values(groupedObj);
const result = idiomas.map((idioma, i) => ({
...idioma,
quizz: grouped[i]
}));
But when i do a return result i have this:
[
{
"__setters__": [
"$attributes",
"$persisted",
"primaryKeyValue",
"$originalAttributes",
"$relations",
"$sideLoaded",
"$parent",
"$frozen",
"$visible",
"$hidden"
],
"$attributes": {
"id": 2,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30T13:04:47.000Z",
"updated_at": "2020-01-05T19:08:00.000Z",
"language_substring": "US"
},
"$persisted": true,
"$originalAttributes": {
"id": 2,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30T13:04:47.000Z",
"updated_at": "2020-01-05T19:08:00.000Z",
"language_substring": "US"
},
"$relations": {},
"$sideLoaded": {},
"$parent": null,
"$frozen": false,
"quizz": [
{
"id": 1,
"book_id": 1,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 2,
"book_id": 1,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 3,
"book_id": 1,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
}
]
},
{
"__setters__": [
"$attributes",
"$persisted",
"primaryKeyValue",
"$originalAttributes",
"$relations",
"$sideLoaded",
"$parent",
"$frozen",
"$visible",
"$hidden"
],
"$attributes": {
"id": 3,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30T14:06:44.000Z",
"updated_at": "2019-12-30T14:06:44.000Z",
"language_substring": null
},
"$persisted": true,
"$originalAttributes": {
"id": 3,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30T14:06:44.000Z",
"updated_at": "2019-12-30T14:06:44.000Z",
"language_substring": null
},
"$relations": {},
"$sideLoaded": {},
"$parent": null,
"$frozen": false,
"quizz": [
{
"id": 4,
"book_id": 2,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
}
]
How i can deserialize this? JSON.stringify(result) returns the same json.
Try this:
const result = idiomas.map(({ $attributes }, i) => ({
...$attributes,
quizz: grouped[i]
}))
What is the query to update below data with mongoose. So 3 fields are going to be updated. Top Parent Points, Categories Points and Tag Points.
{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
"points": 45,
"level": 1,
"categories": [
{
"name": "Computer Science",
"points": 15,
"level": 1,
"_id": "561fba5e7fac41a4055fad46",
"tags": [
{
"name": "C#",
"points": 10,
"level": 1,
"_id": "561fba5e7fac41a4055fad47"
},
{
"name": "Java",
"points": 5,
"level": 1,
"_id": "561fba5e7fac41a4055ert12"
}
]
},
{
"name": "History",
"points": 30,
"level": 2,
"_id": "562407d4e3edf2113f61ac37",
"tags": [
{
"name": "WW2",
"points": 30,
"level": 2,
"_id": "56240797e3edf2113f61ac36"
}
]
}
]
}
to this one. When user gets a point from a specific tag, it will effect all parents. Let's say, user gets 10 points from C# then i have to update mongodb to this.
{
"_id": "561fba5e7fac41a4055fad45",
"fullName": "Test",
**"points": 55,**
"level": 1,
"categories": [
{
"name": "Computer Science",
**"points": 25,**
"level": 1,
"_id": "561fba5e7fac41a4055fad46",
"tags": [
{
"name": "c#",
**"points": 20,**
"level": 1,
"_id": "561fba5e7fac41a4055fad47"
},
{
"name": "Java",
"points": 5,
"level": 1,
"_id": "561fba5e7fac41a4055ert12"
}
]
},
{
"name": "History",
"points": 30,
"level": 2,
"_id": "562407d4e3edf2113f61ac37",
"tags": [
{
"name": "WW2",
"points": 30,
"level": 2,
"_id": "56240797e3edf2113f61ac36"
}
]
}
]
}
you should use $elemMatch for querying your object
db.tests.update({_id: yourTestId, categories: {$elemMatch: {_id: categoryId}}}, {$set: {$inc: {"categories.$.points": 10, points: 10}}})
So you querying only needed array element and update it values with $ reference