Related
I need to loop through and print all nested data within the "players" array and sort by "wonAmount." I can get my PHP to print out a single value, which I target, but I can't get it to work when I try the foreach loop. So instead, it prints out "array."
JSON
{
"tournamentID": 65,
"gameType": "Holdem",
"name": "TRI KRALJA ZA EVROPU",
"start": "2022-04-07 13:30:00",
"status": "Finished",
"buyIn": 100000,
"entryFee": 10000,
"guaranteedPrize": 12500000,
"rebuyFee": 100000,
"rebuyRake": 10000,
"addonFee": 0,
"addonRake": 0,
"tableSize": 9,
"speed": "Turbo",
"players": [
{
"id": 1000000,
"alias": "demo1",
"rank": 1,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 6250000
},
{
"id": 1000005,
"alias": "demo6",
"rank": 2,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 3750000
},
{
"id": 1000087,
"alias": "demo10",
"rank": 3,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 2500000
},
{
"id": 1000008,
"alias": "demo9",
"rank": 4,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000078,
"alias": "demo15",
"rank": 5,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000002,
"alias": "demo3",
"rank": 6,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000006,
"alias": "demo7",
"rank": 7,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000090,
"alias": "demo13",
"rank": 8,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000171,
"alias": "demo14",
"rank": 9,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000088,
"alias": "okokoke",
"rank": 10,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000007,
"alias": "demo8",
"rank": 11,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000086,
"alias": "demo11",
"rank": 12,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000004,
"alias": "demo5",
"rank": 13,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000001,
"alias": "demo2",
"rank": 14,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000003,
"alias": "demo4",
"rank": 15,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
},
{
"id": 1000077,
"alias": "helloworld",
"rank": 16,
"rebuyCount": 0,
"addonCount": 0,
"wonAmount": 0
}
]
}
first, you need to decode the json using json_decode then you need to sort & print the player's array
$data = json_decode($json_data,true);
//extract the players from json
$players = $data["players"];
//sort players array
$wonAmount = array();
foreach ($players as $key => $row)
{
$wonAmount[$key] = $row['wonAmount'];
}
array_multisort($wonAmount, SORT_DESC, $players);
//print players data
foreach ($players as $player)
{
//add here any keys you want to print from player array
echo $player["id"];
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
This is my json in postman:
[
[
{
"amount_refunded": 1,
"base_amount_refunded": 1,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 1,
"base_price": 1,
"base_price_incl_tax": 1,
"base_row_invoiced": 1,
"base_row_total": 1,
"base_row_total_incl_tax": 1,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"base_tax_refunded": 0,
"created_at": "2021-10-04 13:02:36",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 166,
"name": "Configurable Product 16 - option 21",
"no_discount": 0,
"order_id": 85,
"original_price": 1,
"price": 1,
"price_incl_tax": 1,
"product_id": 1234,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 164,
"row_invoiced": 1,
"row_total": 1,
"row_total_incl_tax": 1,
"row_weight": 1,
"sku": "Configurable Product 16 - option 21",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"tax_refunded": 0,
"updated_at": "2021-10-15 09:28:44",
"weee_tax_applied": "[]",
"weight": 1
}
],
[
{
"amount_refunded": 80,
"applied_rule_ids": "1",
"base_amount_refunded": 80,
"base_discount_amount": 8,
"base_discount_invoiced": 8,
"base_discount_refunded": 8,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 80,
"base_price": 80,
"base_price_incl_tax": 86.6,
"base_row_invoiced": 80,
"base_row_total": 72,
"base_row_total_incl_tax": 77.94,
"base_tax_amount": 5.94,
"base_tax_invoiced": 5.94,
"base_tax_refunded": 5.94,
"created_at": "2021-10-15 11:23:54",
"discount_amount": 8,
"discount_invoiced": 8,
"discount_percent": 10,
"discount_refunded": 8,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 169,
"name": "Ski vest 92",
"no_discount": 0,
"order_id": 88,
"original_price": 80,
"price": 80,
"price_incl_tax": 86.6,
"product_id": 1267,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 171,
"row_invoiced": 80,
"row_total": 72,
"row_total_incl_tax": 77.94,
"row_weight": 5,
"sku": "Ski vest 92",
"store_id": 1,
"tax_amount": 5.94,
"tax_invoiced": 5.94,
"tax_percent": 8.25,
"tax_refunded": 5.94,
"updated_at": "2021-10-15 11:25:07",
"weee_tax_applied": "[]",
"weight": 5
},
{
"amount_refunded": 0,
"applied_rule_ids": "1",
"base_amount_refunded": 0,
"base_discount_amount": 2,
"base_discount_invoiced": 2,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_original_price": 20,
"base_price": 20,
"base_price_incl_tax": 20,
"base_row_invoiced": 20,
"base_row_total": 18,
"base_row_total_incl_tax": 18,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"created_at": "2021-10-15 11:23:54",
"discount_amount": 2,
"discount_invoiced": 2,
"discount_percent": 10,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 170,
"name": "testcron",
"no_discount": 0,
"order_id": 88,
"original_price": 20,
"price": 20,
"price_incl_tax": 20,
"product_id": 1260,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 1,
"quote_item_id": 173,
"row_invoiced": 20,
"row_total": 18,
"row_total_incl_tax": 18,
"row_weight": 2,
"sku": "testcron",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"updated_at": "2021-10-15 11:24:39",
"weee_tax_applied": "[]",
"weight": 2
},
{
"amount_refunded": 0,
"applied_rule_ids": "1",
"base_amount_refunded": 0,
"base_discount_amount": 0.5,
"base_discount_invoiced": 0.5,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_original_price": 5,
"base_price": 5,
"base_price_incl_tax": 5.41,
"base_row_invoiced": 5,
"base_row_total": 4.5,
"base_row_total_incl_tax": 4.87,
"base_tax_amount": 0.37,
"base_tax_invoiced": 0.37,
"created_at": "2021-10-15 11:23:54",
"discount_amount": 0.5,
"discount_invoiced": 0.5,
"discount_percent": 10,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 171,
"name": "time",
"no_discount": 0,
"order_id": 88,
"original_price": 5,
"price": 5,
"price_incl_tax": 5.41,
"product_id": 1261,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 0,
"qty_shipped": 1,
"quote_item_id": 174,
"row_invoiced": 5,
"row_total": 4.5,
"row_total_incl_tax": 4.87,
"row_weight": 2,
"sku": "time",
"store_id": 1,
"tax_amount": 0.37,
"tax_invoiced": 0.37,
"tax_percent": 8.25,
"updated_at": "2021-10-15 11:24:39",
"weee_tax_applied": "[]",
"weight": 2
}
],
[
{
"amount_refunded": 80,
"base_amount_refunded": 80,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 80,
"base_price": 80,
"base_price_incl_tax": 80,
"base_row_invoiced": 80,
"base_row_total": 80,
"base_row_total_incl_tax": 80,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"base_tax_refunded": 0,
"created_at": "2021-10-19 08:49:35",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 172,
"name": "Ski vest 92",
"no_discount": 0,
"order_id": 89,
"original_price": 80,
"price": 80,
"price_incl_tax": 80,
"product_id": 1267,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 175,
"row_invoiced": 80,
"row_total": 80,
"row_total_incl_tax": 80,
"row_weight": 5,
"sku": "Ski vest 92",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"tax_refunded": 0,
"updated_at": "2021-10-19 09:06:19",
"weee_tax_applied": "[]",
"weight": 5
},
{
"amount_refunded": 5,
"base_amount_refunded": 5,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 5,
"base_price": 5,
"base_price_incl_tax": 5,
"base_row_invoiced": 5,
"base_row_total": 5,
"base_row_total_incl_tax": 5,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"base_tax_refunded": 0,
"created_at": "2021-10-19 08:49:35",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 173,
"name": "time",
"no_discount": 0,
"order_id": 89,
"original_price": 5,
"price": 5,
"price_incl_tax": 5,
"product_id": 1261,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 177,
"row_invoiced": 5,
"row_total": 5,
"row_total_incl_tax": 5,
"row_weight": 2,
"sku": "time",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"tax_refunded": 0,
"updated_at": "2021-10-19 09:06:19",
"weee_tax_applied": "[]",
"weight": 2
}
],
[
{
"amount_refunded": 5,
"base_amount_refunded": 5,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 5,
"base_price": 5,
"base_price_incl_tax": 5.41,
"base_row_invoiced": 5,
"base_row_total": 5,
"base_row_total_incl_tax": 5.41,
"base_tax_amount": 0.41,
"base_tax_invoiced": 0.41,
"base_tax_refunded": 0.41,
"created_at": "2021-10-19 10:27:53",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 176,
"name": "Configurable Product 16",
"no_discount": 0,
"order_id": 91,
"original_price": 5,
"price": 5,
"price_incl_tax": 5.41,
"product_id": 1253,
"product_type": "configurable",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 180,
"row_invoiced": 5,
"row_total": 5,
"row_total_incl_tax": 5.41,
"row_weight": 1,
"sku": "Configurable Product 16 - option 9",
"store_id": 1,
"tax_amount": 0.41,
"tax_invoiced": 0.41,
"tax_percent": 8.25,
"tax_refunded": 0.41,
"updated_at": "2021-10-19 10:50:02",
"weee_tax_applied": "[]",
"weight": 1,
"product_option": {
"extension_attributes": {
"configurable_item_options": [
{
"option_id": "212",
"option_value": 478
},
{
"option_id": "213",
"option_value": 480
}
]
}
}
},
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_price": 0,
"base_row_invoiced": 0,
"base_row_total": 0,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"base_tax_refunded": 0,
"created_at": "2021-10-19 10:27:53",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 177,
"name": "Configurable Product 16 - option 9",
"no_discount": 0,
"order_id": 91,
"original_price": 0,
"parent_item_id": 176,
"price": 0,
"product_id": 1222,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 0,
"quote_item_id": 181,
"row_invoiced": 0,
"row_total": 0,
"row_weight": 0,
"sku": "Configurable Product 16 - option 9",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"tax_refunded": 0,
"updated_at": "2021-10-19 10:50:02",
"weight": 1,
"parent_item": {
"amount_refunded": 5,
"base_amount_refunded": 5,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_refunded": 0,
"base_discount_tax_compensation_amount": 0,
"base_discount_tax_compensation_invoiced": 0,
"base_discount_tax_compensation_refunded": 0,
"base_original_price": 5,
"base_price": 5,
"base_price_incl_tax": 5.41,
"base_row_invoiced": 5,
"base_row_total": 5,
"base_row_total_incl_tax": 5.41,
"base_tax_amount": 0.41,
"base_tax_invoiced": 0.41,
"base_tax_refunded": 0.41,
"created_at": "2021-10-19 10:27:53",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"discount_refunded": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"discount_tax_compensation_invoiced": 0,
"discount_tax_compensation_refunded": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 176,
"name": "Configurable Product 16",
"no_discount": 0,
"order_id": 91,
"original_price": 5,
"price": 5,
"price_incl_tax": 5.41,
"product_id": 1253,
"product_type": "configurable",
"qty_canceled": 0,
"qty_invoiced": 1,
"qty_ordered": 1,
"qty_refunded": 1,
"qty_shipped": 1,
"quote_item_id": 180,
"row_invoiced": 5,
"row_total": 5,
"row_total_incl_tax": 5.41,
"row_weight": 1,
"sku": "Configurable Product 16 - option 9",
"store_id": 1,
"tax_amount": 0.41,
"tax_invoiced": 0.41,
"tax_percent": 8.25,
"tax_refunded": 0.41,
"updated_at": "2021-10-19 10:50:02",
"weee_tax_applied": "[]",
"weight": 1,
"product_option": {
"extension_attributes": {
"configurable_item_options": [
{
"option_id": "212",
"option_value": 478
},
{
"option_id": "213",
"option_value": 480
}
]
}
}
},
"row_total_incl_tax": 0,
"base_row_total_incl_tax": 0
}
]
]
I am trying to loop threough this json so I can get all the "amount_refunded values".
This is the code that I have now but I don't understand how I can get the amount_refunded values.
Note that ->items is used and is correct because it needs to get all the items.
public function getRefundedOrders(Request $request)
{
try{
$response = Http::withToken($request->post('token'))
->get($request->post('URL').'/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=base_subtotal_refunded&searchCriteria[filter_groups][0][filters][0][condition_type]=gt');
}
catch(Throwaway $e){
Log::error($e);
}
$refunds = json_decode($response);
$user = Users::whereUrl($request->post('URL'))->firstOrFail();
$refundedItems = [];
foreach($refunds->items as $product){
array_push($refundedItems, $product->items);
}
$refundedProducts = [];
if (isset($response['message']))
{
return response()->json(['type' => 'error', 'message' => $response['message']]);
}
return $refundedItems;
}
I don't understand how I can loop through this so I can get all the amount_refunded from every object inside of this json.
Assuming your json is missing a closing "]", then decode it into an associative array using :
$string = '--yourjsongoeshere--';
$data = json_decode($string, true);
then set your total refunds to 0, loop through the arrays and add the total refunds to it.
$totalrefund = 0;
foreach($data as $datum) {
foreach($datum as $entry) {
$totalrefund += $entry['amount_refunded'];
}
}
echo $totalrefund;
You can use Laravel collection to get an array of amount_refunded.
$refunds = collect(json_decode($response))
->flatten()
->pluck('amount_refunded')
->toArray();
To get all data filtered by amount_refunded > 0
$refunds = collect(json_decode($response))->flatten()->filter(function ($item) {
return $item->amount_refunded > 0;
})->toArray();
Docs: https://laravel.com/docs/8.x/collections#available-methods
Assuming the JSON you've shown is the contents of $response, there is no "items" mentioned anywhere in the JSON, so it's not clear why you included that or why you think it's working. It would definitely cause errors / warnings if you tried to use the code you've shown with that data.
Your JSON structure is an outer array, with multiple smaller arrays inside it - and each of those smaller arrays can have one or more entries.
This simple code will just output the value of each "amount_refunded" field, by looping through the outer array, and then looping through each of the inner ones:
$refunds = json_decode($response);
foreach($refunds as $refund) {
foreach ($refund as $item)
echo $item->amount_refunded.PHP_EOL; //PHP_EOL just adds a newline character. Use <br/> instead for HTML output.
}
Working demo: http://sandbox.onlinephpfunctions.com/code/728b81aa408b811b9d98a0c01d3dc782c4f05951
I am trying to get the order list using the below url,
GET METHOD - http://magento.local/index.php/rest/V1/orders?searchCriteria
Header: Authorization: Bearer TOKEN(Customer Token)
But its giving the following response,
{
"message": "Consumer is not authorized to access %resources",
"parameters": {
"resources": "Magento_Sales::sales"
}
}
what needs to be done to get the order list using customer token?
Unfortunately it's not implemented in Magento_Sales::sales yet, but you can extend your own API. follow the following issue:
https://github.com/magento/magento2/issues/3552
For the get Customer Order history use searchCriteria and passed customer_email id.
Follow below Magento 2 API to get the customer order history.
Request URL:
https://magento.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value]=test#gmail.com
Method: GET
Response:
{
"items": [
{
"base_currency_code": "KWD",
"base_discount_amount": 0,
"base_grand_total": 40,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 10,
"base_shipping_discount_amount": 0,
"base_shipping_discount_tax_compensation_amnt": 0,
"base_shipping_incl_tax": 10,
"base_shipping_tax_amount": 0,
"base_subtotal": 30,
"base_subtotal_incl_tax": 30,
"base_tax_amount": 0,
"base_total_due": 40,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 250,
"created_at": "2019-08-14 11:26:48",
"customer_email": "test#gmail.com",
"customer_firstname": "Test",
"customer_group_id": 1,
"customer_id": 51,
"customer_is_guest": 0,
"customer_lastname": "Customer",
"customer_note_notify": 1,
"discount_amount": 0,
"email_sent": 1,
"entity_id": 125,
"global_currency_code": "KWD",
"grand_total": 40,
"discount_tax_compensation_amount": 0,
"increment_id": "000000119",
"is_virtual": 0,
"order_currency_code": "KWD",
"protect_code": "9a7b171724410d42aec193ba5206901e",
"quote_id": 398,
"remote_ip": "202.131.115.180",
"shipping_amount": 10,
"shipping_description": "Flat Rate - Fixed",
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 10,
"shipping_tax_amount": 0,
"state": "new",
"status": "pending",
"store_currency_code": "KWD",
"store_id": 1,
"store_name": "Default Store",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 30,
"subtotal_incl_tax": 30,
"tax_amount": 0,
"total_due": 40,
"total_item_count": 1,
"total_qty_ordered": 2,
"updated_at": "2019-08-14 11:26:49",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_cost": 8.5,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 15,
"base_price": 15,
"base_price_incl_tax": 15,
"base_row_invoiced": 0,
"base_row_total": 30,
"base_row_total_incl_tax": 30,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"created_at": "2019-08-14 11:26:48",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 341,
"name": "DIAMOND GLITTER GRAY",
"no_discount": 0,
"order_id": 125,
"original_price": 15,
"price": 15,
"price_incl_tax": 15,
"product_id": 228,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 2,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 1281,
"row_invoiced": 0,
"row_total": 30,
"row_total_incl_tax": 30,
"row_weight": 0,
"sku": "CLBELGLG0175DCMO",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"updated_at": "2019-08-14 11:26:48"
}
],
"billing_address": {
"address_type": "billing",
"city": "California",
"company": "Company",
"country_id": "US",
"email": "test#gmail.com",
"entity_id": 250,
"firstname": "john",
"lastname": "harrison",
"parent_id": 125,
"postcode": "12345",
"region": "New York",
"region_code": "NY",
"region_id": 43,
"street": [
"Street Address"
],
"telephone": "2313131312"
},
"payment": {
"account_status": null,
"additional_information": [
"Check / Money order"
],
"amount_ordered": 40,
"base_amount_ordered": 40,
"base_shipping_amount": 10,
"cc_exp_year": "0",
"cc_last4": null,
"cc_ss_start_month": "0",
"cc_ss_start_year": "0",
"entity_id": 125,
"method": "checkmo",
"parent_id": 125,
"shipping_amount": 10
},
"status_histories": [],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"address": {
"address_type": "shipping",
"city": "California",
"company": "Company",
"country_id": "US",
"email": "test#gmail.com",
"entity_id": 249,
"firstname": "john",
"lastname": "harrison",
"parent_id": 125,
"postcode": "12345",
"region": "New York",
"region_code": "NY",
"region_id": 43,
"street": [
"Street Address"
],
"telephone": "2313131312"
},
"method": "flatrate_flatrate",
"total": {
"base_shipping_amount": 10,
"base_shipping_discount_amount": 0,
"base_shipping_discount_tax_compensation_amnt": 0,
"base_shipping_incl_tax": 10,
"base_shipping_tax_amount": 0,
"shipping_amount": 10,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 10,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"base_amount_refunded": 0,
"base_cost": 8.5,
"base_discount_amount": 0,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 15,
"base_price": 15,
"base_price_incl_tax": 15,
"base_row_invoiced": 0,
"base_row_total": 30,
"base_row_total_incl_tax": 30,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"created_at": "2019-08-14 11:26:48",
"discount_amount": 0,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 341,
"name": "BELLA DIAMOND GLITTER GRAY 0175",
"no_discount": 0,
"order_id": 125,
"original_price": 15,
"price": 15,
"price_incl_tax": 15,
"product_id": 228,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 2,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 1281,
"row_invoiced": 0,
"row_total": 30,
"row_total_incl_tax": 30,
"row_weight": 0,
"sku": "CLBELGLG0175DCMO",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"updated_at": "2019-08-14 11:26:48"
}
]
}
],
"rewards_discount": 0,
"rewards_spend": 0,
"rewards_earn": 950
}
},
{
"applied_rule_ids": "4,11",
"base_currency_code": "KWD",
"base_discount_amount": -15,
"base_grand_total": 165,
"base_discount_tax_compensation_amount": 0,
"base_shipping_amount": 45,
"base_shipping_discount_amount": 0,
"base_shipping_discount_tax_compensation_amnt": 0,
"base_shipping_incl_tax": 45,
"base_shipping_tax_amount": 0,
"base_subtotal": 135,
"base_subtotal_incl_tax": 135,
"base_tax_amount": 0,
"base_total_due": 165,
"base_to_global_rate": 1,
"base_to_order_rate": 1,
"billing_address_id": 268,
"created_at": "2019-09-24 13:16:32",
"customer_email": "test#gmail.com",
"customer_firstname": "Test",
"customer_group_id": 1,
"customer_id": 51,
"customer_is_guest": 0,
"customer_lastname": "Customer",
"customer_note_notify": 1,
"discount_amount": -15,
"email_sent": 1,
"entity_id": 134,
"global_currency_code": "KWD",
"grand_total": 165,
"discount_tax_compensation_amount": 0,
"increment_id": "000000128",
"is_virtual": 0,
"order_currency_code": "KWD",
"protect_code": "1deccd785628b2df682d9156db732fc0",
"quote_id": 408,
"shipping_amount": 45,
"shipping_description": "Flat Rate - Fixed",
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 45,
"shipping_tax_amount": 0,
"state": "new",
"status": "pending",
"store_currency_code": "KWD",
"store_id": 1,
"store_name": "Default Store",
"store_to_base_rate": 0,
"store_to_order_rate": 0,
"subtotal": 135,
"subtotal_incl_tax": 135,
"tax_amount": 0,
"total_due": 165,
"total_item_count": 1,
"total_qty_ordered": 9,
"updated_at": "2019-09-24 13:16:33",
"weight": 0,
"items": [
{
"amount_refunded": 0,
"applied_rule_ids": "4,11",
"base_amount_refunded": 0,
"base_cost": 8.5,
"base_discount_amount": 15,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 15,
"base_price": 15,
"base_price_incl_tax": 15,
"base_row_invoiced": 0,
"base_row_total": 135,
"base_row_total_incl_tax": 135,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"created_at": "2019-09-24 13:16:32",
"discount_amount": 15,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 351,
"name": "BELLA DIAMOND GLITTER GRAY 0175",
"no_discount": 0,
"order_id": 134,
"original_price": 15,
"price": 15,
"price_incl_tax": 15,
"product_id": 228,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 9,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 1378,
"row_invoiced": 0,
"row_total": 135,
"row_total_incl_tax": 135,
"row_weight": 0,
"sku": "CLBELGLG0175DCMO",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"updated_at": "2019-09-24 13:16:32"
}
],
"billing_address": {
"address_type": "billing",
"city": "California",
"company": "Company",
"country_id": "US",
"email": "test#gmail.com",
"entity_id": 268,
"firstname": "john",
"lastname": "harrison",
"parent_id": 134,
"postcode": "12345",
"region": "New York",
"region_code": "NY",
"region_id": 43,
"street": [
"Street Address"
],
"telephone": "2313131312"
},
"payment": {
"account_status": null,
"additional_information": [
"Check / Money order"
],
"amount_ordered": 165,
"base_amount_ordered": 165,
"base_shipping_amount": 45,
"cc_exp_year": "0",
"cc_last4": null,
"cc_ss_start_month": "0",
"cc_ss_start_year": "0",
"entity_id": 134,
"method": "checkmo",
"parent_id": 134,
"shipping_amount": 45
},
"status_histories": [],
"extension_attributes": {
"shipping_assignments": [
{
"shipping": {
"address": {
"address_type": "shipping",
"city": "California",
"company": "Company",
"country_id": "US",
"email": "test#gmail.com",
"entity_id": 267,
"firstname": "john",
"lastname": "harrison",
"parent_id": 134,
"postcode": "12345",
"region": "New York",
"region_code": "NY",
"region_id": 43,
"street": [
"Street Address"
],
"telephone": "2313131312"
},
"method": "flatrate_flatrate",
"total": {
"base_shipping_amount": 45,
"base_shipping_discount_amount": 0,
"base_shipping_discount_tax_compensation_amnt": 0,
"base_shipping_incl_tax": 45,
"base_shipping_tax_amount": 0,
"shipping_amount": 45,
"shipping_discount_amount": 0,
"shipping_discount_tax_compensation_amount": 0,
"shipping_incl_tax": 45,
"shipping_tax_amount": 0
}
},
"items": [
{
"amount_refunded": 0,
"applied_rule_ids": "4,11",
"base_amount_refunded": 0,
"base_cost": 8.5,
"base_discount_amount": 15,
"base_discount_invoiced": 0,
"base_discount_tax_compensation_amount": 0,
"base_original_price": 15,
"base_price": 15,
"base_price_incl_tax": 15,
"base_row_invoiced": 0,
"base_row_total": 135,
"base_row_total_incl_tax": 135,
"base_tax_amount": 0,
"base_tax_invoiced": 0,
"created_at": "2019-09-24 13:16:32",
"discount_amount": 15,
"discount_invoiced": 0,
"discount_percent": 0,
"free_shipping": 0,
"discount_tax_compensation_amount": 0,
"is_qty_decimal": 0,
"is_virtual": 0,
"item_id": 351,
"name": "BELLA DIAMOND GLITTER GRAY 0175",
"no_discount": 0,
"order_id": 134,
"original_price": 15,
"price": 15,
"price_incl_tax": 15,
"product_id": 228,
"product_type": "simple",
"qty_canceled": 0,
"qty_invoiced": 0,
"qty_ordered": 9,
"qty_refunded": 0,
"qty_shipped": 0,
"quote_item_id": 1378,
"row_invoiced": 0,
"row_total": 135,
"row_total_incl_tax": 135,
"row_weight": 0,
"sku": "CLBELGLG0175DCMO",
"store_id": 1,
"tax_amount": 0,
"tax_invoiced": 0,
"tax_percent": 0,
"updated_at": "2019-09-24 13:16:32"
}
]
}
],
"rewards_discount": 0,
"rewards_spend": 0,
"rewards_earn": 3770
}
}
],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "customer_email",
"value": "test#gmail.com",
"condition_type": "eq"
}
]
}
]
},
"total_count": 2
}
I have collection of 113 documents. It is data from departments of university. There are 3 types of departments:
natural science departments => "chairtype" = "E",
humanities departments => "chairtype" = "G",
creative departments => "chairtype" = "T"
Every document contains fields with the same names:
"mijczjeqeo"
"vmfqvfjptu"
"fwtweaeeba"
...
and so on
113 people fill these fields from the web form every month. 1 person fills 1 document. After filling I make history of values in every document by php script.
"mijczjeqeo" value moves to "versions.0.content.mijczjeqeo"
"vmfqvfjptu" value moves to "versions.0.content.vmfqvfjptu"
"fwtweaeeba" value moves to "versions.0.content.fwtweaeeba"
...
and so on
and before saving last values...
all of "versions.0.content." values move to "versions.1.content."
all of "versions.1.content." values move to "versions.2.content."
all of "versions.2.content." values move to "versions.3.content."
...
and so on
I need to get the average value of each parameter for the last year grouped by chairtype
1 document for example ("Department of Organic Chemistry"):
{
"_id": ObjectId("52b85dfa32b6249513f15897"),
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 0,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"chairtype": "E",
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 1,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 0,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 3,
"fwtweaeeba": 52.31,
"fybnnlojgb": 5,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 2,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 9000000,
"jncncbplme": 3,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 1,
"ojklibfieg": 2,
"padotysmxb": 0,
"parent": "47de3176-bbc3-44e0-8063-8920ac56fdc8",
"pidwyruvfq": 35.08,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 1,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"title_ru": "Кафедра органической химии",
"tmbagkmlgb": 0,
"type": "chair",
"uploaded": "1",
"uqcdessbeu": 0,
"url": "http:\/\/www.herzen.spb.ru\/main\/structure\/fukultets\/him\/1208434887\/",
"uuid": "a0a39ace-694c-48c5-841d-8b351e5b91da",
"vacoxpronz": 0,
"vdjfydjrpa": 13,
"versions": {
"0": {
"_id": ObjectId("52dbbc4cfb0a29ce4a8b45bd"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 0,
"awquatbduv": 0,
"axdducvoxb": 3,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"confirmed": null,
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 2,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 3,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 55,
"fybnnlojgb": 0,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 3,
"hchpoxxnwz": 1849020,
"hmorfnbfvf": 2,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 2,
"ikxsvguboy": 1,
"ipjpwkbqnt": 0,
"izqighabwk": 1040000,
"jncncbplme": 1,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 1,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 2,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 1,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 11,
"ojklibfieg": 0,
"padotysmxb": 0,
"pidwyruvfq": 34,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 1,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 5,
"wewmtafjvk": 1,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 0,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3
},
"content_hash": "816090f397962f92f5329fa5bb0a9ec1",
"datetime": ISODate("2014-01-19T11:51:40.590Z"),
"description_ru": "Значение показателей за этап 1",
"label_ru": "Окончание этапа 1"
},
"1": {
"_id": ObjectId("5305372cfb0a2944638b45bc"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 1,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 1,
"cclwyezydc": 0,
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 0,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 52.31,
"fybnnlojgb": 0,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 0,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 0,
"jncncbplme": 0,
"jxkspszlrc": 1,
"kekarveuhb": 1,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 0,
"ojklibfieg": 1,
"padotysmxb": 0,
"pidwyruvfq": 34.15,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 0,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"visited": null,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 0,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 0,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
},
"content_hash": "d273fb095a7c08fef69fb90ec316fcb9",
"datetime": ISODate("2014-02-19T22:58:52.805Z"),
"description_ru": "Значение показателей за этап 2",
"label_ru": "Окончание этапа 2"
},
"2": {
"_id": ObjectId("532854a3fb0a2973718b45c6"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 2,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 1,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 52.15,
"fybnnlojgb": 5,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 2,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 0,
"jncncbplme": 0,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 0,
"ojklibfieg": 1,
"padotysmxb": 0,
"pidwyruvfq": 34.62,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 2,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 0,
"uploaded": null,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"visited": true,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 0,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 1,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 2,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
},
"content_hash": "64adcf5534b5b1f77282a95f0b14ef99",
"datetime": ISODate("2014-03-18T14:13:55.593Z"),
"description_ru": "Значение показателей за этап 3",
"document_id": "52b85dfa32b6249513f15897",
"document_uuid": "a0a39ace-694c-48c5-841d-8b351e5b91da",
"label_ru": "Окончание этапа 3"
}
},
"visited": true,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 1,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 1,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
}
Now earliest array with history of data is "versions.2.", but 2 weeks later it will be "versions.3." and 6 weeks later it will be "versions.4." and so on...
**this is average of "versions.0.vofeebpgsc" values grouped by "chairtype":**
array(2) {
["result"]=>
array(3) {
[0]=>
array(2) {
["_id"]=>
string(1) "E"
["MID"]=>
float(1.3903333333333)
}
[1]=>
array(2) {
["_id"]=>
string(1) "T"
["MID"]=>
float(0.4)
}
[2]=>
array(2) {
["_id"]=>
string(1) "G"
["MID"]=>
float(0.72931034482759)
}
}
["ok"]=>
float(1)
}
function getMiddle($itemName,$chairType){
//return average of "versions.0.$itemName" value for "chairtype" = $chairType
switch ($chairType){
case 'E':
$chairType = 0;
break;
case 'T':
$chairType = 1;
break;
case 'G':
$chairType = 2;
break;
}
$m = new MongoClient();
$db = $m->foo_data;
$collection = new MongoCollection($db, 'documents');
$thisField = '$versions.content.'.$itemName;
$out = $collection->aggregate(
array('$match' => array('type' => 'chair')
),
array('$unwind' => '$versions'),
array('$group' => array( '_id' => '$_id',
'chairtype' => array('$first' =>'$chairtype'),
'versions' => array('$first' => '$versions')
)),
array('$group'=> array( '_id'=>'$chairtype',
'MID'=> array('$avg'=> $thisField)
))
);
return round ($out['result'][$chairType]['MID'],2);
}
echo getMiddle('vofeebpgsc','G'); //I use it in foreach cycle and send different input values
I need to get average value for each parameter between "versions.0.param_name" and "versions.11.param_name" in every document grouped by "chairtype" (it will be average from parameter history for last year for each type of university department)
I got this task because the average value of "versions.0.param_name" is not very good reflects the statistics.
for example... I have 60 documents with "chairtype" = "E" and now I have '0','1' and '2' arrays of history in versions
I need to calculate:
(
"versions.0.content.fwtweaeeba" +
"versions.1.content.fwtweaeeba" +
"versions.2.content.fwtweaeeba" +
the same fields for each of 59 documents
) / (60*3) = It is good average of "fwtweaeeba" parameter for "chairtype" = "E"
9 months later I will have 60 documents with "chairtype" = "E" and I will have '0','1','2','3','4','5','6','7',8','9','10','11', arrays of history in versions
I will need to calculate:
(
"versions.0.content.fwtweaeeba" +
"versions.1.content.fwtweaeeba" +
"versions.2.content.fwtweaeeba" +
"versions.3.content.fwtweaeeba" +
"versions.4.content.fwtweaeeba" +
"versions.5.content.fwtweaeeba" +
"versions.6.content.fwtweaeeba" +
"versions.7.content.fwtweaeeba" +
"versions.8.content.fwtweaeeba" +
"versions.9.content.fwtweaeeba" +
"versions.10.content.fwtweaeeba" +
"versions.11.content.fwtweaeeba" +
the same fields for each of 59 documents
) / (60*12) = It will be good average of "fwtweaeeba" parameter for "chairtype" = "E"
and so on for each "chairtype"
Can I do it with mongo db aggregation framework?
How can I do it?
Сan anyone suggest a good HOWTO about compiling difficult aggregation queries in MongoDB?
The data structure here is not a good implementation, there are lots of problems with how this is structured and it is completely unsuited to aggregation. The main problems here are:
Your structure does not actually use any arrays, right now it does not
All of the specific key names case a real problem, and this can be avoided.
As such the only way to traverse this sort of structure is using JavaScript with mapReduce.
Defining a mapper:
var mapper = function () {
for ( var n in this.versions ) {
for ( var k in this.versions[n].content ) {
if (
( k != 'confirmed' ) ||
( k != 'visited' ) )
emit(
{
type: this.chairtype,
key: k
},
this.versions[n].content[k]
);
}
}
};
So what this is doing is cycling through each of the versions entries and then also through everything on content. The key is emitted for each of the content keys you want as well as by the "chairtype" key. And the value is that matching value.
And then a reducer:
var reducer = function (key,values) {
return ( Array.sum( values ) != 0 )
? Array.sum( values ) / values.length : 0;
};
Which is just a simple way of producing a an average from all the values coming in for the mapper with the same key.
So while that should work nicely, what you should be doing is changing your structure. So in fact if you had something like this:
{
"_id": ObjectId("52b85dfa32b6249513f15897"),
"parent": "47de3176-bbc3-44e0-8063-8920ac56fdc8",
"type": "chair",
"chairtype": "E",
"content": [
{ "key": "atkswlntfd", "value": 0, "version": 0 },
{ "key": "auwbsjqzir", "value": 0, "version": 0 },
{ "key": "avqrnjzbgd", "value": 0, "version": 0 }
]
}
Or generally more or less in that form, the the aggregation operation becomes very simple:
db.collection.aggregate([
{ "$unwind": "$content" },
{ "$group": {
"_id": {
"chairtype": "$chairtype",
"key": "$content.key"
},
"average": { "$avg": "$content.value" }
}}
])
Or whatever other variation of this is required, but now it is made possible by changing the structure.
So without the document being structured differently you will need to use mapReduce to do this.
I have written a below code which generates a area graph for selected dates (to and from dates).
$(document).ready(function() {
var options = {
"series": [{
"showInLegend": false,
"color": "#D0D0D0",
"name": "Revenue",
"data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}],
"credits": {
"enabled": false
},
"chart": {
"renderTo": "highchart_id",
"defaultSeriesType": "area",
"shadow": true
},
"title": {
"text": null,
"align": "center",
"x": 0,
"y": 20
},
"xAxis": {
"type": "datetime",
"dateTimeLabelFormats": {
"month": "%e. %b",
"year": "%b"
},
"labels": {
"enabled": "false"
}
},
"legend": {
"enabled": true
},
"yAxis": {
"title": {
"text": ""
},
"labels": {
"enabled": true
}
},
"plotOptions": {
"area": {
"stacking": "normal",
"lineColor": "#3E3E3E",
"lineWidth": 3,
"marker": {
"lineWidth": "1",
"lineColor": "#3E3E3E",
"states": {
"hover": {
"enabled": true,
"radius": 1
}
}
}
},
"series": {
"pointStart": 1335823200000,
"pointInterval": 86400000
}
}
};
var chart = new Highcharts.Chart(options);
});
The interval of dates on x-axis is coming one day less. here the series value 2 is for 10 may whereas it is showing for 9 may in graph.
I am new to this highchart, can anybody advice the solution please and let me known if you need any more clarification on the problem.
Try adding this code before declaring the chart
Highcharts.setOptions({
time: {
useUTC: false
}
});
By default highchart used UTC times. By adding the code above it will use the time zone of the browser.