MongoDB Chart $multiply - arrays

I have a data like this.
"products": [{
"_id": {
"$oid": "5ffd0a8f6273740017cc5fca"
},
"name": "Banana",
"price": 65,
"createdAt": {
"$date": "2021-01-12T02:33:51.648Z"
},
"updatedAt": {
"$date": "2021-01-12T02:33:51.648Z"
},
"quantity": 3
}, {
"_id": {
"$oid": "5ffd09326273740017cc5fb3"
},
"name": "Apple",
"price": 79,
"createdAt": {
"$date": "2021-01-12T02:28:02.412Z"
},
"updatedAt": {
"$date": "2021-01-12T02:28:02.412Z"
},
"quantity": 2
}]
What I'm trying to do is multiply the price over the quantity.
Implementation:
{ $reduce: {
input: '$products', initialValue: 0,
in: { $multiply: ["$products.price",
"$products.quantity"] } }
I'm having an error $multiply only supports numeric types, not array

Related

I'm new to this, What am I doing wrong? Creating array of line items in JSON and getting parse error. - Expecting 'STRING', got '{'

enter image description here
Parse error on line 15:
...,
"Qty": 3
},
{
"Amount":
--------------------^
Expecting 'STRING', got '{'
I don't know how to format the code for stack overflow but this is the JSON array I'm trying to put together
{
"MetaData": {
"CreateTime": "2019-05-16T18:13:13-08:00",
"LastUpdatedTime": "2019-05-16T18:13:45-08:00"
},
"Line": [{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
},
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
} }
],
"CustomerRef": {
"value": "20"
}
}
The solution is probably simple
{
"MetaData": {
"CreateTime": "2019-05-16T18:13:13-08:00",
"LastUpdatedTime": "2019-05-16T18:13:45-08:00"
},
"Line": [
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
} <--- MISSING BRACE HERE
},
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
}
}
],
"CustomerRef": {
"value": "20"
}
}

Elasticsearch query: combine nested array of objects into one array

Using Elasticsearch I am trying to combine a nested array of objects into one array.
This is what my data looks like:
GET invoices/_search
{
"hits": [
{
"_index": "invoices",
"_id": "1234",
"_score": 1.0,
"_source": {
"id": 1234,
"status": "unpaid",
"total": 15.35,
"payments": [
{
"id": 1981,
"amount": 10,
"date": "2022-02-09T13:00:00+01:00"
},
{
"id": 1982,
"amount": 5.35,
"date": "2022-02-09T13:35:00+01:00"
}
]
}
},
# ... More hits
]
}
I want to only get the payments array of each hit combined into one array, so that it returns something like this:
{
"payments": [
{
"id": 1981,
"amount": 10,
"date": "2022-02-09T13:00:00+01:00"
},
{
"id": 1982,
"amount": 5.35,
"date": "2022-02-09T13:35:00+01:00"
},
{
"id": 5658,
"amount": 3,
"date": "2021-12-19T13:00:00+01:00"
}
]
}
I tried to get this result using nested queries but could not figure it out, the query I used is:
# Query I used:
GET invoices/_search
{
"_source": ["payments"],
"query": {
"nested": {
"path": "payments",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "payments.id"
}
}
]
}
}
}
}
}
# Result:
{
"hits": [
{
"_index": "invoices",
"_id": "545960",
"_score": 1.0,
"_source": {
"payments": [
{
"date": "2022-01-22T15:38:15+01:00",
"amount": 374.5,
"id": 320320
},
{
"date": "2022-01-22T15:30:03+01:00",
"amount": 160.5,
"id": 320316
}
]
}
},
{
"_index": "invoices",
"_id": "545961",
"_score": 1.0,
"_source": {
"payments": [
{
"date": "2022-01-22T15:38:15+01:00",
"amount": 12,
"id": 320350
},
{
"date": "2022-01-22T15:30:03+01:00",
"amount": 60.65,
"id": 320379
}
]
}
}
]
}
The result returns only the payments array but divided over multiple hits. How can I combine those arrays?

calculate the sum from sub array of objects for individual object in mongoose

How to calculate the sum of confident_score for every individual vendor?
Data stored in the DB:
[
{
"_id": "61cab38891152daf9387c0c7",
"name": "dummy",
"company_email": "abc#mailinator.com",
"brief_msg": "Cillum sed est prae",
"similar_case_ids": [],
"answer_id": [
"61cab38891152daf9387c0c9"
],
"pros_cons": [
{
"vendor_name": "xyzlab",
"score": [
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor F",
"confident_score": 80,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf0f"
},
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor FFF",
"confident_score": 40,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf10"
}
]
},
{
"vendor_name": "abclab",
"score": [
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor B",
"confident_score": 50,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf16"
},
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor BB",
"confident_score": 60,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf17"
}
]
}
]
the query for getting the matching id and grouping objects according to the vendor_name:
aggregate([
{
$match: { _id: id }
},
{
$unwind: {
path: '$pros_cons'
}
},
{
$group: {
_id: '$pros_cons'
}
},
])
};
After query I'm getting this:
[
{
"_id": {
"vendor_name": "abclab",
"score": [
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor B",
"confident_score": 50,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf16"
},
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor BB",
"confident_score": 60,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf17"
}
],
}
},
{
"_id": {
"vendor_name": "xyzlab",
"score": [
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor F",
"confident_score": 80,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf0f"
},
{
"question_id": "61c5b47198b2c5bbf9f6471c",
"title": "Vendor FFF",
"confident_score": 40,
"text": "text1",
"_id": "61cac505caeeeb3cec78bf10"
}
],
}
}
]
Need to calculate sum for (vendor_name:abclab)TOTAL=110 and for (vendor_name:xyzlab)TOTAL=120 INDIVIDUALLY
required output:
[
{
"vendor_name": "abclab",
"totalScore": 110,
"count" : 2
},
{
"vendor_name": "xyzlab",
"totalScore": 120,
"count" : 2
}
]
$match - Filter documents by id.
$unwind - Deconstruct pros_cons array to multiple documents.
$project - Decorate output documents. With $reduce, to create totalScore field by summing confident_score from each element in pros_cons.score array.
db.collection.aggregate([
{
$match: {
_id: "61cab38891152daf9387c0c7"
}
},
{
$unwind: {
path: "$pros_cons"
}
},
{
$project: {
_id: 0,
vendor_name: "$pros_cons.vendor_name",
totalScore: {
$reduce: {
input: "$pros_cons.score",
initialValue: 0,
in: {
$sum: [
"$$value",
"$$this.confident_score"
]
}
}
}
}
}
])
Sample Demo on Mongo Playground

MongoDB - Returning only one matching array element from Object

One of my project collection.
{
"id": {
"$oid": "5f4600ab7ec81f6c20f8608d"
},
"name": "2",
"category": "2",
"description": "2",
"deadline": "2020-08-10",
"createdBy": {
"$oid": "5f5089a2265ec85b896f848f"
},
"__v": 116,
"todos": [{
"time": {
"$date": "2020-09-10T06:15:36.168Z"
},
"doneAt": {
"$date": "2020-09-10T16:51:53.534Z"
},
"done": true,
"_id": {
"$oid": "5f59c4f53f0095593b2ace25"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "ooooooo"
}, {
"time": {
"$date": "2020-09-05T13:11:14.139Z"
},
"doneAt": null,
"done": false,
"_id": {
"$oid": "5f538f1eeef4d614830e843c"
},
"user": {
"$oid": "5f5089a2265ec85b896f848f"
},
"text": "11"
}],
"bugs": [{
"time": {
"$date": "2020-09-09T03:30:45.463Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f584f152c5530129ff36ea2"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "new bug"
}, {
"time": {
"$date": "2020-09-06T09:35:12.848Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f54e1220297a57896646e98"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "4"
}, {
"time": {
"$date": "2020-09-06T09:35:12.848Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f54e0fc0297a57896646e97"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "5"
}],
"members": [{
"_id": {
"$oid": "5f57725977ead80a566855e4"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
}
}, {
"_id": {
"$oid": "5f576452fe05ec34a1bc487b"
},
"user": {
"$oid": "5f4c8654f4b8c6671d5926ac"
}
}, {
"_id": {
"$oid": "5f562a971e16aa39ca290391"
},
"user": {
"$oid": "5f5089a2265ec85b896f848f"
}
}]
}
There are more project collection. I put only one here. My goal is retrive one bug element
such as
{
"time": {
"$date": "2020-09-09T03:30:45.463Z"
},
"fixedAt": null,
"fixed": false,
"_id": {
"$oid": "5f584f152c5530129ff36ea2"
},
"user": {
"$oid": "5f3fcbcae5efa018cc3c9b9d"
},
"text": "new bug"
}
Every project collection and bug element has own id. I just want to retrieve one bug element from an bugs array in project object using mongoose.
Every project object has similar structure.

I can't convert object to an array

I am trying to make a shopping cart API route and I am stuck with this problem because when I am using this statement $carts = \Cart::session($session)->getContent();
I get an object like this:
"cart": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
and I want to convet this to be array of objects. I tryied $cart->toArray(); and didn't work, also I tiyed (array) $cart and I get this:
"cart": {
"\u0000*\u0000items": {
"1": {
"id": 1,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": "1",
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg"
},
"conditions": []
},
"2": {
"id": 2,
"name": "Tort cu biscuiți",
"price": 20,
"quantity": 2,
"attributes": {
"image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg"
},
"conditions": []
}
}
}
may I know what is the problem?
You can do it manuel like this:
$carts = \Cart::session($session)->getContent();
if ($carts) {
$cartsArray = json_decode($carts);
foreach($cartsArray->cart as &$cart){
$cart = (array) $cart;
}
}
dd($cartsArray);

Resources