pivot sub array of data pivottable.js - pivot-table

I am using pivottable to create and display dynamic report and I would like to know is there any way to pivot the data in sub array on the json?
Example data:
<script type="text/javascript">
// This example is the most basic usage of pivot()
var data = [
{
"accountId": "50000X",
"productId": 1,
"name": "PRODUCT A",
"sku": "SKU-A",
"fulfillmentSku": "SKU-A",
"friendlySku": "SKU-A",
"quantityStart": 19524,
"quantityEnd": 18523,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": -1
},
{
"eventType": "Deduction",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": -1500
},
{
"eventType": "Received",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 500
}
]
},
{
"accountId": "50000X",
"productId": 97,
"name": "PRODUCT B",
"sku": "SKU-B",
"fulfillmentSku": null,
"friendlySku": "SKU-B",
"quantityStart": -22,
"quantityEnd": 48,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 60
},
{
"eventType": "Received",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 10
}
]
},
{
"accountId": "50000X",
"productId": 96,
"name": "PRODUCT C",
"sku": "SKU-C",
"fulfillmentSku": null,
"friendlySku": "SKU-C",
"quantityStart": 2755,
"quantityEnd": 2755,
"activity": []
},
{
"accountId": "50000X",
"productId": 95,
"name": "PRODUCT D",
"sku": "SKU-C",
"fulfillmentSku": null,
"friendlySku": "SKU-C",
"quantityStart": -11,
"quantityEnd": -6,
"activity": [
{
"eventType": "Assemby",
"createdOnUtc": "2018-01-26T00:00:00",
"quantity": 5
}
]
}
];
$(function () {
$("#output").pivot(data,
{
rows: ["productId", "name", "sku", "quantityStart", "quantityEnd"],
cols: ["activity"]
}
);
});
</script>
I would like to pivot the activity data, everything in the activity is grouped and computed.
or do I have to change my json format to get the expected output?

PivotTable.js cannot handle nested arrays like this (see the documentation here with the note about "scalar values not objects") so you will need to restructure your input.

Related

Grouping a collection IN LARAVEL

I have an array called $customerRecords. I want to group the data in this array by the customer's email.
This is the array below
$customerRecords = [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
]
This is the expected result I want to achieve so that I can know the group of data that belongs to an email .
{
"doe#mailnator.com": [
{
"id": 1,
"note": "This is note 1",
"customer": [
{
"id": 1,
"user_id": 34,
"email": "doe#mailnator.com",
"phone": "9829484857"
}
]
}
],
"john#mailnator.com": [
{
"id": 2,
"note": "This is note 2",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
},
{
"id": 3,
"note": "This is a note 3",
"customer": [
{
"id": 2,
"user_id": 34,
"email": "john#mailnator.com",
"phone": "9829484857"
}
]
}
]
}
So this is what I have tried but it's not working:
return collect($customerRecords)->groupBy('customer.email')
you are almost done just define customer 0 item then email
return collect($customerRecords)->groupBy('customer.0.email');
This is how I was able to solve it.
$grouped = [];
foreach($customerRecords as $value) {
foreach($value['customer'] as $cust) {
$grouped[$cust['email']][] = $value;
}
}

MongoDB Chart $multiply

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

Solr get facets on each root document

I have a JSON with documents and each document has a few _childDocuments_ I want to search in the parent and get back the parents and facets on each parent separate
Here is a sample JSON
[{
"id": 1,
"productName": "Ford Explorer",
"_childDocuments_": [{
"id": 6,
"color": "blue",
"price": 1000
},
{
"id": 7,
"color": "red",
"price": 2000
}
]
},
{
"id": 1,
"productName": "Ford F150",
"_childDocuments_": [{
"id": 10,
"color": "blue",
"price": 5000
},
{
"id": 11,
"color": "red",
"price": 6000
}
]
},
{
"id": 2,
"productName": "Toyota Highlander",
"_childDocuments_": [{
"id": 8,
"color": "green",
"price": 1200
},
{
"id": 9,
"color": "red",
"price": 2000
}
]
}
]
I want when I search for Ford I should get back all Fords and in each root document get back facets of the children.
Something like this:
[{
"id": 1,
"productName": "Ford Explorer",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 2000,
"count": 1
},
{
"val": 3000,
"count": 1
}
]
}
}
},
{
"id": 1,
"productName": "Ford F150",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 5000,
"count": 1
},
{
"val": 6000,
"count": 1
}
]
}
}
}
]
This is what I got so far
q={!parent+which=type:parent}&json.facet={ "prices": {
"type": "range",
"field": "price",
"start": 1000,
"end": 1000,
"gap": 1000
}
,
}
But this is only returning facets on all child documents.
Thanks in advance

How to get a value in one mongodb collection and use that value to update another document in another collection order & inventory system

Hello I have been stuck for weeks trying to figure how to create a order & inventory system for a project I am working on. I don't know how to properly ask this but my problem is when a user adds items to their cart > I store the order details in a orders collection in mongodb > I then need to figure out how to subtract the quantity of the items in a customers order from my inventory collection. How can I do this with mongodb, Python
This is the document created when a customer places an order
{
"_id": "5eca94b4f56331fd9eade681",
"ordernumber": 343,
"order": {
"order_details": [
{
"customer_info": [
{
"first_name": "John",
"last_name": "Doe",
"email": "email#email.com"
}
],
"shipping_details": [
{
"shipping_address": "Test Address",
"shipping_zip": "12345",
"shippingl_city": "Test city",
"shipping_country": "USA"
}
],
"products_ordered": [
{
"variant_id": "a",
"product_name": "red shirt",
"price": 30,
"quantity": 2,
"image": "imageurl",
"size": "Small"
},
{
"variant_id": "f",
"product_name": "Blue Jeans",
"price": 20,
"quantity": 3,
"image": "imageurl",
"size": "Large"
}
]
}
]
}
}
These are the products in my inventory collection I want inventory order quantity subtracted by the quantity a customer purchased
{
"_id": "5eca0ff4898b8f30a9fee5e5",
"product_id": 1,
"product_name": "red shirt",
"category": "shirts",
"price": 30,
"status": "instock",
"description": "nice red shirt",
"alt": "string",
"images": [
"imageUrl1",
"imageUrl2"
],
"variants": [
{
"Small": [
{
"variant_id": "a",
"inventory": 30
}
],
"Medium": [
{
"variant_id": "b",
"inventory": 10
}
],
"Large": [
{
"variant_id": "c",
"inventory": 10
}
]
}
]
}
{
"_id": "5eca108f898b8f30a9fee5e6",
"product_id": 2,
"product_name": "blue jeans",
"category": "jeans",
"price": 20,
"status": "instock",
"description": "nice blue jeans",
"alt": "string",
"images": [
"ImageURL"
],
"variants": [
{
"Small": [
{
"variant_id": "d",
"inventory": 100
}
],
"Medium": [
{
"variant_id": "e",
"inventory": 150
}
],
"Large": [
{
"variant_id": "f",
"inventory": 70
}
] }
]
}
I would suggest to do it along with the service which creates the order.
I would also like to suggest to refactor the db structure a bit as it would be harder to maintain this in a larger scale.
Because currently we would have to write something like
for ordered_product in products_ordered:
query = { "product_name": ordered_product.get("product_name") }
inventory_product = inventory_collection.find_one(query)
product_id = inventory_product["_id"]
existing_count = inventory_product["variants"][0][ordered_product.size][0]["inventory"]
inventory_product["variants"][0][ordered_product["size"]][0]["inventory"] = existing_count - ordered_product["quantity"]
inventory_collection.update_one({ "_id": product_id }, { "$set": inventory_product })
I have hardcoded the index values of the list. You could use filter() to filter out the variant and size you need.
This code definitely seems messy to me.
Of course you could refactor this code by splitting it into functions inside the model file itself, but I would suggest to refactor the db structure for better scalability.
May be you could move the variants to a seperate collection and use the product_id as a link. You have to think this through before getting on with the code.
Hope this helps.

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