Vega custom tooltip data visualisation - visualization

Could anyone help be to develop a tooltip at line chart that looks like this?
Here is my spec at Vega Lite Editor.

Tooltips work on key:value pairs. If you amend your input data to an object of key value pairs and create a flatten transform, you can achieve your desired behaviour. I have changed all the tooltips below to be identical for speed but you should see the pattern.
{
"description": "Total Count line chart.",
"width": 1200,
"height": 450,
"padding": 5,
"signals": [{"name": "interpolate", "value": "linear"}],
"legends": [
{
"fill": "color",
"orient": "bottom",
"direction": "horizontal",
"symbolType": "square"
}
],
"data": [
{
"name": "table",
"values": [
{
"x": 0,
"y": 30,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 1,
"y": 20,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 2,
"y": 90,
"c": "Passenger Vessel",
"tooltip": [
{
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 3,
"y": 60,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 4,
"y": 50,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 5,
"y": 40,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 6,
"y": 10,
"c": "Passenger Vessel",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 0,
"y": 50,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 1,
"y": 10,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 2,
"y": 50,
"c": "Pleasure Craft",
"tooltip": [
{
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 3,
"y": 40,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 4,
"y": 110,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 5,
"y": 40,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{
"x": 6,
"y": 20,
"c": "Pleasure Craft",
"tooltip": [
{
"title": "My Title",
"Tall Ship": 0,
"Sailing Yacht": 10,
"Super Yacht": 20,
"Motor Yacht": 10,
"Rib": 0
}
]
},
{"x": 0, "y": 50, "c": "Unknown", "tooltip": [{}]},
{"x": 1, "y": 60, "c": "Unknown", "tooltip": [{}]},
{"x": 2, "y": 90, "c": "Unknown", "tooltip": [{}]},
{"x": 3, "y": 40, "c": "Unknown", "tooltip": [{}]},
{"x": 4, "y": 50, "c": "Unknown", "tooltip": [{}]},
{"x": 5, "y": 20, "c": "Unknown", "tooltip": [{}]},
{"x": 6, "y": 40, "c": "Unknown", "tooltip": [{}]}
],
"transform": [{"type": "flatten", "fields": ["tooltip"]}]
}
],
"scales": [
{
"name": "x",
"type": "point",
"range": "width",
"domain": {"data": "table", "field": "x"}
},
{
"name": "y",
"type": "linear",
"range": "height",
"nice": true,
"zero": true,
"domain": {"data": "table", "field": "y"}
},
{
"name": "color",
"type": "ordinal",
"range": ["#BA20CE", "#60cf85", "#cd2c4f"],
"domain": {"data": "table", "field": "c"}
}
],
"axes": [
{"orient": "bottom", "scale": "x"},
{"orient": "left", "scale": "y"}
],
"config": {
"style": {
"guide-label": {"fontSize": 14, "fill": "#cccccc", "fontWeight": 800}
},
"axis": {"grid": true, "gridColor": "#333333"}
},
"marks": [
{
"type": "group",
"from": {"facet": {"name": "series", "data": "table", "groupby": "c"}},
"marks": [
{
"type": "line",
"from": {"data": "series"},
"encode": {
"enter": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"stroke": {"scale": "color", "field": "c"},
"strokeWidth": {"value": 2}
},
"update": {
"interpolate": {"signal": "interpolate"},
"strokeOpacity": {"value": 1}
},
"hover": {"strokeOpacity": {"value": 0.5}}
}
},
{
"type": "symbol",
"from": {"data": "series"},
"encode": {
"update": {
"x": {"scale": "x", "field": "x"},
"y": {"scale": "y", "field": "y"},
"fillOpacity": {"value": 0}
},
"hover": {
"fillOpacity": {"value": 1},
"fill": {"scale": "color", "field": "c"},
"cursor": {"value": "pointer"},
"tooltip": {"signal": "datum['tooltip'] "}
}
}
}
]
}
]
}

Related

MongoDB aggregation re-group deep nested array of objects

Good day all,
I need some help figuring out this aggregation issue. I have a document with nested arrays where I want to perform lookups on the nested array's values after which I want to restore the document to its original structure. see below:
[
{
"_id": "63c7fec2fe9afea23afdbcef",
"primary_language": "en",
"image_link": "avatar_image_linl",
"description": ["Some description text here"],
"source": "community",
"standard": "imperial",
"gender": "male",
"base_a": 47,
"base_w": 220,
"base_h": 71.65,
"status": "draft",
"date_created": "2023-01-18T14:14:26.201Z",
"product_plan": {
"p_len": 4,
"p_fre": 1,
"p_qua": 4,
"avg_len": 3583,
"total_wor": 16,
"total_exe": 128,
"plan": {
"field_we": 1,
"field_da": 1,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W111",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 3736,
"nbr_exe": 8,
"target_mus": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 16,
"description": "Tricerus"
},
{
"itemID": 5,
"description": "Chiwawak"
}
],
"target_are": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 4,
"description": "Chiwawak"
},
{
"itemID": 2,
"description": "Amanus"
}
],
"items": {
"itemid": 44,
"target_are": [
{
"itemID": 4,
"description": "Chiwawak"
}
],
"target_mus": [
{
"itemID": 5,
"description": "Chiwawak"
}
],
"nbr_ref": 4,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 520,
"_id": "63ca9b798d47745ae5589906",
"status": 0,
"pct_complete": 0,
"set": []
},
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca95368d47745ae558985e"
},
"_id": "63c7fec2fe9afea23afdbcf0"
},
"title": "Short Title text here",
"itemID": "bodyfyme882212-1674051266.214",
"__v": 32
},
{
"_id": "63c7fec2fe9afea23afdbcef",
"primary_language": "en",
"image_link": "avatar_image_linl",
"description": ["Some description text here"],
"source": "community",
"standard": "imperial",
"gender": "male",
"base_a": 47,
"base_w": 220,
"base_h": 71.65,
"status": "draft",
"date_created": "2023-01-18T14:14:26.201Z",
"product_plan": {
"p_len": 4,
"p_fre": 1,
"p_qua": 4,
"avg_len": 3583,
"total_wor": 16,
"total_exe": 128,
"plan": {
"field_we": 1,
"field_da": 1,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W111",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 3736,
"nbr_exe": 8,
"target_mus": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 16,
"description": "Tricerus"
},
{
"itemID": 5,
"description": "Chiwawak"
}
],
"target_are": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 4,
"description": "Chiwawak"
},
{
"itemID": 2,
"description": "Amanus"
}
],
"items": {
"itemid": 339,
"target_are": [
{
"itemID": 4,
"description": "Chiwawak"
}
],
"target_mus": [
{
"itemID": 5,
"description": "Chiwawak"
}
],
"nbr_ref": 3,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 390,
"_id": "63ca9b798d47745ae5589907",
"status": 0,
"pct_complete": 0,
"set": []
},
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca95368d47745ae558985e"
},
"_id": "63c7fec2fe9afea23afdbcf0"
},
"title": "Short Title text here",
"itemID": "bodyfyme882212-1674051266.214",
"__v": 32
},
{
"_id": "63c7fec2fe9afea23afdbcef",
"primary_language": "en",
"image_link": "avatar_image_linl",
"description": ["Some description text here"],
"source": "community",
"standard": "imperial",
"gender": "male",
"base_a": 47,
"base_w": 220,
"base_h": 71.65,
"status": "draft",
"date_created": "2023-01-18T14:14:26.201Z",
"product_plan": {
"p_len": 4,
"p_fre": 1,
"p_qua": 4,
"avg_len": 3583,
"total_wor": 16,
"total_exe": 128,
"plan": {
"field_we": 1,
"field_da": 2,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W121",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 2674,
"nbr_exe": 6,
"target_mus": [
{
"itemID": 9,
"description": "Lats"
},
{
"itemID": 10,
"description": "Lower Back"
},
{
"itemID": 11,
"description": "Middle Back"
}
],
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"items": {
"itemid": 47,
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"target_mus": [
{
"itemID": 10,
"description": "Lower Back"
}
],
"nbr_ref": 4,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 520,
"_id": "63caa1568d47745ae5589b25",
"status": 0,
"pct_complete": 0,
"set": []
},
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca9b8e8d47745ae558994b"
},
"_id": "63c7fec2fe9afea23afdbcf0"
},
"title": "Short Title text here",
"itemID": "bodyfyme882212-1674051266.214",
"__v": 32
},
{
"_id": "63c7fec2fe9afea23afdbcef",
"primary_language": "en",
"image_link": "avatar_image_linl",
"description": ["Some description text here"],
"source": "community",
"standard": "imperial",
"gender": "male",
"base_a": 47,
"base_w": 220,
"base_h": 71.65,
"status": "draft",
"date_created": "2023-01-18T14:14:26.201Z",
"product_plan": {
"p_len": 4,
"p_fre": 1,
"p_qua": 4,
"avg_len": 3583,
"total_wor": 16,
"total_exe": 128,
"plan": {
"field_we": 1,
"field_da": 2,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W121",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 2674,
"nbr_exe": 6,
"target_mus": [
{
"itemID": 9,
"description": "Lats"
},
{
"itemID": 10,
"description": "Lower Back"
},
{
"itemID": 11,
"description": "Middle Back"
}
],
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"items": {
"itemid": 495,
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"target_mus": [
{
"itemID": 11,
"description": "Middle Back"
}
],
"nbr_ref": 3,
"int_rec": "10~12",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 414,
"_id": "63caa1568d47745ae5589b26",
"status": 0,
"pct_complete": 0,
"set": []
},
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca9b8e8d47745ae558994b"
},
"_id": "63c7fec2fe9afea23afdbcf0"
},
"title": "Short Title text here",
"itemID": "bodyfyme882212-1674051266.214",
"__v": 32
},
{
......
}
]
Now i want to get the document back to this structure using aggregation.
{
"_id": "63c7fec2fe9afea23afdbcef",
"primary_language": "en",
"image_link": "avatar_image_linl",
"description": ["Some description text here"],
"source": "community",
"standard": "imperial",
"gender": "male",
"base_a": 47,
"base_w": 220,
"base_h": 71.65,
"status": "draft",
"date_created": "2023-01-18T14:14:26.201Z",
"product_plan": {
"p_len": 4,
"p_fre": 1,
"p_qua": 4,
"avg_len": 3583,
"total_wor": 16,
"total_exe": 128,
// Group plan here by product_plan._id
"plan": [
{
"field_we": 1,
"field_da": 1,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W111",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 3736,
"nbr_exe": 8,
"target_mus": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 16,
"description": "Tricerus"
},
{
"itemID": 5,
"description": "Chiwawak"
}
],
"target_are": [
{
"itemID": 1,
"description": "Adominorus"
},
{
"itemID": 4,
"description": "Chiwawak"
},
{
"itemID": 2,
"description": "Amanus"
}
],
// group items by plan._id
"items": [
{
"itemid": 44,
"target_are": [
{
"itemID": 4,
"description": "Chiwawak"
}
],
"target_mus": [
{
"itemID": 5,
"description": "Chiwawak"
}
],
"nbr_ref": 4,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 520,
"_id": "63ca9b798d47745ae5589906",
"status": 0,
"pct_complete": 0,
"set": []
},
{
"itemid": 339,
"target_are": [
{
"itemID": 4,
"description": "Chiwawak"
}
],
"target_mus": [
{
"itemID": 5,
"description": "Chiwawak"
}
],
"nbr_ref": 3,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 390,
"_id": "63ca9b798d47745ae5589907",
"status": 0,
"pct_complete": 0,
"set": []
},
],
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca95368d47745ae558985e"
},
{
"field_we": 1,
"field_da": 2,
"field_ti": 1,
"field_rest_we": false,
"field_rest_da": false,
"product": {
"name": "W121",
"comment": {
"en": ""
},
"pro_diff": [
{
"itemID": 2,
"description": "Intermediate"
}
],
"pro_int": [
{
"itemID": 2,
"description": "Moderate"
}
],
"pro_dur": 2674,
"nbr_exe": 6,
"target_mus": [
{
"itemID": 9,
"description": "Lats"
},
{
"itemID": 10,
"description": "Lower Back"
},
{
"itemID": 11,
"description": "Middle Back"
}
],
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
// group items by plan._id
"items": [
{
"itemid": 47,
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"target_mus": [
{
"itemID": 10,
"description": "Lower Back"
}
],
"nbr_ref": 4,
"int_rec": "06~10",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 520,
"_id": "63caa1568d47745ae5589b25",
"status": 0,
"pct_complete": 0,
"set": []
},
{
"itemid": 495,
"target_are": [
{
"itemID": 3,
"description": "Back"
}
],
"target_mus": [
{
"itemID": 11,
"description": "Middle Back"
}
],
"nbr_ref": 3,
"int_rec": "10~12",
"type_rec": [
{
"itemID": 14,
"description": "Straight Set"
}
],
"tempo": "1-1-1-1",
"rest_time": 90,
"note": "",
"order": null,
"pro_dur": 414,
"_id": "63caa1568d47745ae5589b26",
"status": 0,
"pct_complete": 0,
"set": []
}
],
"isCustomName": false,
"status": 0,
"pct_complete": 0
},
"_id": "63ca9b8e8d47745ae558994b"
}
],
"_id": "63c7fec2fe9afea23afdbcf0"
},
"title": "Short Title text here",
"itemID": "bodyfyme882212-1674051266.214",
"__v": 32
}
Any help would be greatly appreciated

Flutter: Populate listview with headers and items from complex list

I have following List:
[
{
"ID": "1",
"ParentID": "0",
"CategoryName": "FourWheeler",
"Children": [
{
"ID": "9",
"ParentID": "1",
"CategoryName": "Jeep",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "10",
"ParentID": "1",
"CategoryName": "Taxi",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "11",
"ParentID": "1",
"CategoryName": "Car",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "12",
"ParentID": "1",
"CategoryName": "Van",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "13",
"ParentID": "1",
"CategoryName": "Other",
"ParentCategoryName": "FourWheeler"
}]
},
{
"ID": "2",
"ParentID": "0",
"CategoryName": "Boat",
"Children": [
{
"ID": "14",
"ParentID": "2",
"CategoryName": "Motorboat",
"ParentCategoryName": "Boat"
},
{
"ID": "15",
"ParentID": "2",
"CategoryName": "Sailingboat",
"ParentCategoryName": "Boat"
},
{
"ID": "16",
"ParentID": "2",
"CategoryName": "SteamBoat",
"ParentCategoryName": "Boat"
},
{
"ID": "17",
"ParentID": "2",
"CategoryName": "Other",
"ParentCategoryName": "Boat"
}]
}
]
I need to populate ListView on the basis of this list. We should have listview populated such a way that there are headers and each headers will have their respective items.
For Example, ListView should look something like,
**FourWheeler**
Jeep
Taxi
Car
Van
Other
**Boat**
Motorboat
Sailingboat
Steamboat
Other
For simple list like this:
List Fruits = ['Apple','Orange','Kiwi','Avocado'];
I would have done like this
Fruits.map<Widget>((fruit)=>Container(child: Text(fruit))).toList();
But I don't know how to deal with the scenario I have. Any help will be appreciated. Thanks
This is one way you can populate a ListView as you have described:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListView(
children: [
for (final header in data) ...[
ListTile(title: Text('**${header['CategoryName']}**')),
for (final item in header['Children'] ?? [])
ListTile(title: Text(' ${item['CategoryName']}')),
],
],
),
),
);
}
}
const List<Map<String, dynamic>> data = [
{
"ID": "1",
"ParentID": "0",
"CategoryName": "FourWheeler",
"Children": [
{
"ID": "9",
"ParentID": "1",
"CategoryName": "Jeep",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "10",
"ParentID": "1",
"CategoryName": "Taxi",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "11",
"ParentID": "1",
"CategoryName": "Car",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "12",
"ParentID": "1",
"CategoryName": "Van",
"ParentCategoryName": "FourWheeler"
},
{
"ID": "13",
"ParentID": "1",
"CategoryName": "Other",
"ParentCategoryName": "FourWheeler"
}
]
},
{
"ID": "2",
"ParentID": "0",
"CategoryName": "Boat",
"Children": [
{
"ID": "14",
"ParentID": "2",
"CategoryName": "Motorboat",
"ParentCategoryName": "Boat"
},
{
"ID": "15",
"ParentID": "2",
"CategoryName": "Sailingboat",
"ParentCategoryName": "Boat"
},
{
"ID": "16",
"ParentID": "2",
"CategoryName": "SteamBoat",
"ParentCategoryName": "Boat"
},
{
"ID": "17",
"ParentID": "2",
"CategoryName": "Other",
"ParentCategoryName": "Boat"
}
]
}
];
You could also group each header with something like an ExpansionTile:
ListView(
children: [
for (final header in data)
ExpansionTile(
title: Text('**${header['CategoryName']}**'),
children: [
for (final item in header['Children'] ?? [])
ListTile(title: Text(' ${item['CategoryName']}')),
],
),
],
),

Is there a way to merge textures in Unity in a shader?

I'm trying to animate a 2D game object using several overlaid layers of images in one mesh renderer. I've got several layers of different textures, each is an image with a transparent background.
I've found a way to programmatically create a rectangular mesh and layer the materials within it with UV mapping. Unfortunately Unity now has to render each of these material layers separately, despite the fact that they are all within one mesh. This results in a very inefficient number of draw calls. I can see that each material now has it's own shader as well.
Will I need to edit all of my images into one gigantic image outside of Unity and display portions of them using UV mapping in a single material within the mesh?
Or is there some way to achieve this with a shader?
Create your own Shader (Code or ShaderGraph). Shaders can render multiple Textures (layers). You can blend by alpha/transprency however you like.
Edit: Example in ShaderGraph:
ShaderGraph Code:
https://pastebin.com/a8ubgxRP
application/vnd.unity.graphview.elements {
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CopyPasteGraph",
"m_ObjectId": "82a0e513542e4106ae94a0ba8a6ec750",
"m_Edges": [
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "a58e7e104e604e0b9e2961da5510e2bf"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "5e1616a6e87c470f8e3520b520b86bea"
},
"m_SlotId": 1
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "5e1616a6e87c470f8e3520b520b86bea"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "b9bc9d71a2354d1ebadb93dab18e7223"
},
"m_SlotId": 0
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "e5668fa7ac4e42fdaa32049802bd78b2"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "a275a2c058614973b0efd13817919cc6"
},
"m_SlotId": 1
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "a275a2c058614973b0efd13817919cc6"
},
"m_SlotId": 0
},
"m_InputSlot": {
"m_Node": {
"m_Id": "b9bc9d71a2354d1ebadb93dab18e7223"
},
"m_SlotId": 1
}
},
{
"m_OutputSlot": {
"m_Node": {
"m_Id": "a275a2c058614973b0efd13817919cc6"
},
"m_SlotId": 7
},
"m_InputSlot": {
"m_Node": {
"m_Id": "b9bc9d71a2354d1ebadb93dab18e7223"
},
"m_SlotId": 3
}
}
],
"m_Nodes": [
{
"m_Id": "5e1616a6e87c470f8e3520b520b86bea"
},
{
"m_Id": "a275a2c058614973b0efd13817919cc6"
},
{
"m_Id": "e5668fa7ac4e42fdaa32049802bd78b2"
},
{
"m_Id": "b9bc9d71a2354d1ebadb93dab18e7223"
},
{
"m_Id": "a58e7e104e604e0b9e2961da5510e2bf"
}
],
"m_Groups": [],
"m_StickyNotes": [],
"m_Inputs": [],
"m_MetaProperties": [],
"m_MetaPropertyIds": [],
"m_MetaKeywords": [],
"m_MetaKeywordIds": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "09a8291e17ab424e8149728df0325ac7",
"m_Id": 5,
"m_DisplayName": "G",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "G",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "09adddb00a6a45ba89f015d46cc7e777",
"m_Id": 5,
"m_DisplayName": "G",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "G",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot",
"m_ObjectId": "254965f8ab4b456789bf3701f105035f",
"m_Id": 0,
"m_DisplayName": "RGBA",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "RGBA",
"m_StageCapability": 2,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "394d8ab6d6254442a22a1d2a1d393090",
"m_Id": 4,
"m_DisplayName": "R",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "R",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.SampleTexture2DNode",
"m_ObjectId": "5e1616a6e87c470f8e3520b520b86bea",
"m_Group": {
"m_Id": ""
},
"m_Name": "Sample Texture 2D",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1229.6002197265625,
"y": -552.0,
"width": 208.0000762939453,
"height": 433.6000061035156
}
},
"m_Slots": [
{
"m_Id": "7f1789f703db4759a2c2cbbdcee37bb7"
},
{
"m_Id": "fdbf06e354c64748bc51ee1f630dec56"
},
{
"m_Id": "09a8291e17ab424e8149728df0325ac7"
},
{
"m_Id": "e3d48a7642ee490d9e52742319616c2a"
},
{
"m_Id": "6ea82df60cc64a08a6d75d13343fa8b3"
},
{
"m_Id": "fcc275cddbd44f6491e5c6752757e36f"
},
{
"m_Id": "6f90ea7d44914d0985b264b8fdd91f89"
},
{
"m_Id": "f9c9786a2bcd475d9ad02b23429c97f6"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_TextureType": 0,
"m_NormalMapSpace": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.DynamicVectorMaterialSlot",
"m_ObjectId": "5f0e671f32cf47cda2dbaf52f3591216",
"m_Id": 2,
"m_DisplayName": "Out",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Out",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DMaterialSlot",
"m_ObjectId": "69f238b9d6ec46009f9ad1e06532c289",
"m_Id": 0,
"m_DisplayName": "Out",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Out",
"m_StageCapability": 3,
"m_BareResource": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "6a74085b28a4494f9928fbde7bb14ab4",
"m_Id": 6,
"m_DisplayName": "B",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "B",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "6ea82df60cc64a08a6d75d13343fa8b3",
"m_Id": 7,
"m_DisplayName": "A",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "A",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.UVMaterialSlot",
"m_ObjectId": "6f90ea7d44914d0985b264b8fdd91f89",
"m_Id": 2,
"m_DisplayName": "UV",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "UV",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0
},
"m_Labels": [],
"m_Channel": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.DynamicVectorMaterialSlot",
"m_ObjectId": "7d86c10d04514f1db34f1c03b5e0d07a",
"m_Id": 0,
"m_DisplayName": "Base",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Base",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot",
"m_ObjectId": "7f1789f703db4759a2c2cbbdcee37bb7",
"m_Id": 0,
"m_DisplayName": "RGBA",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "RGBA",
"m_StageCapability": 2,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DMaterialSlot",
"m_ObjectId": "9cf09bc550a4468faa6908e33854182c",
"m_Id": 0,
"m_DisplayName": "Out",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "Out",
"m_StageCapability": 3,
"m_BareResource": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.UVMaterialSlot",
"m_ObjectId": "9f7246d6f2f84e50a35232a424585f47",
"m_Id": 2,
"m_DisplayName": "UV",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "UV",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0
},
"m_Labels": [],
"m_Channel": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.SampleTexture2DNode",
"m_ObjectId": "a275a2c058614973b0efd13817919cc6",
"m_Group": {
"m_Id": ""
},
"m_Name": "Sample Texture 2D",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1236.8001708984375,
"y": -95.99998474121094,
"width": 208.00001525878907,
"height": 433.6000061035156
}
},
"m_Slots": [
{
"m_Id": "254965f8ab4b456789bf3701f105035f"
},
{
"m_Id": "394d8ab6d6254442a22a1d2a1d393090"
},
{
"m_Id": "09adddb00a6a45ba89f015d46cc7e777"
},
{
"m_Id": "6a74085b28a4494f9928fbde7bb14ab4"
},
{
"m_Id": "de7a0347276243dca2e037c26fdd8b82"
},
{
"m_Id": "f18732f84c8f40849bb8eb7fc6d31fb6"
},
{
"m_Id": "9f7246d6f2f84e50a35232a424585f47"
},
{
"m_Id": "f08fa5b4eede46afad513c1f5cb4539e"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_TextureType": 0,
"m_NormalMapSpace": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DAssetNode",
"m_ObjectId": "a58e7e104e604e0b9e2961da5510e2bf",
"m_Group": {
"m_Id": ""
},
"m_Name": "Texture 2D Asset",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1484.8001708984375,
"y": -556.7999877929688,
"width": 145.5999755859375,
"height": 105.59998321533203
}
},
"m_Slots": [
{
"m_Id": "9cf09bc550a4468faa6908e33854182c"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_Texture": {
"m_SerializedTexture": "{\"texture\":{\"instanceID\":0}}",
"m_Guid": ""
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.DynamicVectorMaterialSlot",
"m_ObjectId": "aee1f01a0a34437095d6a66540ce346f",
"m_Id": 1,
"m_DisplayName": "Blend",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Blend",
"m_StageCapability": 3,
"m_Value": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
},
"m_DefaultValue": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 0.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlendNode",
"m_ObjectId": "b9bc9d71a2354d1ebadb93dab18e7223",
"m_Group": {
"m_Id": ""
},
"m_Name": "Blend",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -852.800048828125,
"y": -306.3999938964844,
"width": 208.00001525878907,
"height": 360.0
}
},
"m_Slots": [
{
"m_Id": "7d86c10d04514f1db34f1c03b5e0d07a"
},
{
"m_Id": "aee1f01a0a34437095d6a66540ce346f"
},
{
"m_Id": "c868501ed3c14206b2d478b0ceb706bf"
},
{
"m_Id": "5f0e671f32cf47cda2dbaf52f3591216"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_BlendMode": 21
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "c868501ed3c14206b2d478b0ceb706bf",
"m_Id": 3,
"m_DisplayName": "Opacity",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Opacity",
"m_StageCapability": 3,
"m_Value": 1.0,
"m_DefaultValue": 1.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "de7a0347276243dca2e037c26fdd8b82",
"m_Id": 7,
"m_DisplayName": "A",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "A",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "e3d48a7642ee490d9e52742319616c2a",
"m_Id": 6,
"m_DisplayName": "B",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "B",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DAssetNode",
"m_ObjectId": "e5668fa7ac4e42fdaa32049802bd78b2",
"m_Group": {
"m_Id": ""
},
"m_Name": "Texture 2D Asset",
"m_DrawState": {
"m_Expanded": true,
"m_Position": {
"serializedVersion": "2",
"x": -1501.6002197265625,
"y": -96.79998779296875,
"width": 145.60009765625,
"height": 105.5999984741211
}
},
"m_Slots": [
{
"m_Id": "69f238b9d6ec46009f9ad1e06532c289"
}
],
"synonyms": [],
"m_Precision": 0,
"m_PreviewExpanded": true,
"m_PreviewMode": 0,
"m_CustomColors": {
"m_SerializableColors": []
},
"m_Texture": {
"m_SerializedTexture": "{\"texture\":{\"instanceID\":0}}",
"m_Guid": ""
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.SamplerStateMaterialSlot",
"m_ObjectId": "f08fa5b4eede46afad513c1f5cb4539e",
"m_Id": 3,
"m_DisplayName": "Sampler",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Sampler",
"m_StageCapability": 3,
"m_BareResource": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DInputMaterialSlot",
"m_ObjectId": "f18732f84c8f40849bb8eb7fc6d31fb6",
"m_Id": 1,
"m_DisplayName": "Texture",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Texture",
"m_StageCapability": 3,
"m_BareResource": false,
"m_Texture": {
"m_SerializedTexture": "{\"texture\":{\"instanceID\":0}}",
"m_Guid": ""
},
"m_DefaultType": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.SamplerStateMaterialSlot",
"m_ObjectId": "f9c9786a2bcd475d9ad02b23429c97f6",
"m_Id": 3,
"m_DisplayName": "Sampler",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Sampler",
"m_StageCapability": 3,
"m_BareResource": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Texture2DInputMaterialSlot",
"m_ObjectId": "fcc275cddbd44f6491e5c6752757e36f",
"m_Id": 1,
"m_DisplayName": "Texture",
"m_SlotType": 0,
"m_Hidden": false,
"m_ShaderOutputName": "Texture",
"m_StageCapability": 3,
"m_BareResource": false,
"m_Texture": {
"m_SerializedTexture": "{\"texture\":{\"instanceID\":0}}",
"m_Guid": ""
},
"m_DefaultType": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
"m_ObjectId": "fdbf06e354c64748bc51ee1f630dec56",
"m_Id": 4,
"m_DisplayName": "R",
"m_SlotType": 1,
"m_Hidden": false,
"m_ShaderOutputName": "R",
"m_StageCapability": 2,
"m_Value": 0.0,
"m_DefaultValue": 0.0,
"m_Labels": []
}

How to calculate YTD and MTD in mongodb?

How to calculate Month-To-Date(MTD) and Year-To-Date(YTD) in mongodb in a single query? sample data below, in this data requestedOn is a date field, I want to calculate MTD & YTD, on the assumption of financial year on "1st Jan of the year"(For example financial year for year 2016 is "01-Jan-2016" :
{
"_id": {
"$oid": "5808578b33fa6f161c9747f8"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-03-01",
"bookName": "Test6",
"revenue": 10.0,
"unitsSold": 1,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747f9"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-03-01",
"bookName": "Test1",
"revenue": 11.0,
"unitsSold": 2,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747fa"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-06-01",
"bookName": "Test2",
"revenue": 12.0,
"unitsSold": 3,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747fb"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-07-01",
"bookName": "Test3",
"revenue": 13.0,
"unitsSold": 4,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747fc"
},
"_class": "exceltest.TestBean",
"requestedOn": "2009-09-01",
"bookName": "Test4",
"revenue": 14.0,
"unitsSold": 5,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747fd"
},
"_class": "exceltest.TestBean",
"requestedOn": "2009-06-01",
"bookName": "Test5",
"revenue": 15.0,
"unitsSold": 6,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747fe"
},
"_class": "exceltest.TestBean",
"requestedOn": "2004-06-01",
"bookName": "Test10",
"revenue": 16.0,
"unitsSold": 7,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "5808578b33fa6f161c9747ff"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-01-01",
"bookName": "Test11",
"revenue": 100.0,
"unitsSold": 100,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
},
{
"categoryCode": "Cooking/Beverages/Bartending"
},
{
"categoryCode": "Food Receipe/Taste"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e462"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-02-01",
"bookName": "Test1",
"revenue": 20.0,
"unitsSold": 10,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e463"
},
"_class": "exceltest.TestBean",
"requestedOn": "2001-02-01",
"bookName": "Test2",
"revenue": 19.0,
"unitsSold": 9,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e464"
},
"_class": "exceltest.TestBean",
"requestedOn": "2001-02-01",
"bookName": "Test3",
"revenue": 18.0,
"unitsSold": 8,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e465"
},
"_class": "exceltest.TestBean",
"requestedOn": "2007-06-01",
"bookName": "Test4",
"revenue": 17.0,
"unitsSold": 7,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e466"
},
"_class": "exceltest.TestBean",
"requestedOn": "2005-06-01",
"bookName": "Test5",
"revenue": 16.0,
"unitsSold": 6,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e467"
},
"_class": "exceltest.TestBean",
"requestedOn": "2004-06-01",
"bookName": "Test1",
"revenue": 15.0,
"unitsSold": 5,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e468"
},
"_class": "exceltest.TestBean",
"requestedOn": "2002-06-01",
"bookName": "Test2",
"revenue": 14.0,
"unitsSold": 4,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e469"
},
"_class": "exceltest.TestBean",
"requestedOn": "2001-06-01",
"bookName": "Test3",
"revenue": 13.0,
"unitsSold": 3,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e46a"
},
"_class": "exceltest.TestBean",
"requestedOn": "2000-06-01",
"bookName": "Test4",
"revenue": 12.0,
"unitsSold": 2,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}{
"_id": {
"$oid": "580857b833fa6f0c3499e46b"
},
"_class": "exceltest.TestBean",
"requestedOn": "2008-06-01",
"bookName": "Test5",
"revenue": 11.0,
"unitsSold": 1,
"bookCategory": [
{
"categoryCode": "Cooking/"
},
{
"categoryCode": "Cooking/Beverages"
},
{
"categoryCode": "Food Receipe/"
},
{
"categoryCode": "Food Receipe/Bartending"
}
]
}
Regards
Kris
It is a good practice to keep dates in MongoDB in its native dateformat ISODate().
You can use date formats like $year,$month,$day,$hour etc.
These can be used for grouping , in your case:
db.collectionName.aggregate([
{$group:{_id:{'Date':{$year:'$requestedOn'}},total:{$sum:'$FieldName'}}}
])
to convert string to ISODate , answers can be found at
- [http://stackoverflow.com/questions/15473772/how-to-convert-from-string-to-date-data-type?noredirect=1&lq=1][2]
- [http://stackoverflow.com/questions/15473772/how-to-convert-from-string-to-date-data-type?noredirect=1&lq=1][2]

JSONPath extracting values

I want extract all the offerId's from just one of the membershipId's below:
{
"resultCode": "SUCCESS",
"errorCode": null,
"errorMessage": null,
"membershipCoupons":
[
{
"membershipId": "32021428",
"coupons":
[
{
"offerId": "000C291EE8241ED4AAF43058D8564910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8564910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8564910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579699407,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D857A910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D857A910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D857A910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579703400,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D8608910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8608910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8608910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579706707,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D862E910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D862E910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D862E910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579710047,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D87A0910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87A0910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87A0910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579713540,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D877E910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D877E910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D877E910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579717533,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D87B8910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87B8910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87B8910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579721123,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D8870910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8870910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8870910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": 1423579724430,
"redemption": null
},
{
"offerId": "000C297765971EE4A8C22C6999219BA4",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C6999219BA4Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C6999219BA4High.jpg"
}
],
"endTime": 1425168000000,
"activationDate": 1423136616203,
"redemption": null
},
{
"offerId": "000C297765971EE4A8C22C699921FBA4",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C699921FBA4Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C699921FBA4High.jpg"
}
],
"endTime": 1425168000000,
"activationDate": 1423139501440,
"redemption": null
},
{
"offerId": "000C297765971EE4A8C22C6999233BA4",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C6999233BA4Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201504_003/000C297765971EE4A8C22C6999233BA4High.jpg"
}
],
"endTime": 1425168000000,
"activationDate": 1423215586673,
"redemption": null
}
]
},
{
"membershipId": "8315319211",
"coupons":
[
{
"offerId": "000C291EE8241ED4AAF43058D85CA910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85CA910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85CA910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D85F6910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85F6910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85F6910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D85E2910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85E2910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85E2910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D85FC910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85FC910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D85FC910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D8776910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8776910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D8776910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D87A8910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87A8910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87A8910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D87B6910",
"description": "nora",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87B6910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87B6910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
},
{
"offerId": "000C291EE8241ED4AAF43058D87D0910",
"description": "nora.",
"graphics":
[
{
"width": 400,
"height": 200,
"quality": "Low",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87D0910Low.jpg"
},
{
"width": 900,
"height": 600,
"quality": "High",
"url": "https://site/02_201506_011/000C291EE8241ED4AAF43058D87D0910High.jpg"
}
],
"endTime": 1423353600000,
"activationDate": null,
"redemption": null
}
]
}
]
}
I have something that works for both memberId's but I want to parameterize so that I pick only the offerId's for a parameter I have in a session that I usually reference like this ${medlem}, I use Scala.
This works:
.check(jsonPath("$.membershipCoupons[*].coupons[?(#.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))
But how can I say this with a parameter pointing to a specific memberId something like this:
.check(jsonPath("$.membershipCoupons[membershipId[${medlem}]].coupons[?(#.activationDate==null)].offerId").findAll.optional.saveAs("itemList"))
I think you need to split operation on 2 steps:
arrayMembershipId = $.membershipCoupons[*].membershipId
n = getIndex(membershipIdArray, yourcode)
arrayOffer = membershipCoupons[n].coupons[?(#.activationDate==null)].offerId