View pro pozice obsahujici jejich cesty
db.createView("store_positions_complete", "store_positions", [
{
"$graphLookup": {
"from": 'store_positions',
"startWith": '$parent',
"connectFromField": "parent",
"connectToField": "_id",
"as": 'path'
}
},
{"$set": {"path": { "$reverseArray": "$path" }}},
{
"$addFields": {
"path_string": "$path.name"
}
},
{
"$lookup": {
"from": 'warehouse',
"localField": 'warehouse',
"foreignField": '_id',
"as": 'warehouse',
}
},
{
"$set": { "warehouse": { "$first": "$warehouse" }}
},
])
View pro kategorie obsahujici jejich cesti
db.createView("category_complete", "category",
[
{
"$graphLookup": {
"from": 'store_positions',
"startWith": '$parent',
"connectFromField": "parent",
"connectToField": "_id",
"as": 'path'
}
},
{"$set": {"path": { "$reverseArray": "$path" }}},
{
"$addFields": {
"path_string": "$path.name"
}
}
])
View s počtem součástek v sáčcích. Každý dokument odpovídá jedné součástce a v poddokumentu jsou jednotlivé sáčky
db.packets_count_complete.drop();
db.createView("packets_count_complete", "stock", [
{"$unwind": "$packets"},
{"$lookup": { "from": 'store_positions_complete', "localField":'packets.position', "foreignField": '_id', "as": 'packets.position'}},
{"$lookup": { "from": 'stock_operation', "localField":'packets.cid', "foreignField": 'cid', "as": 'component.operations_cid'}},
{"$lookup": { "from": 'stock_operation', "localField":'packets._id', "foreignField": 'pid', "as": 'packets.operations'}},
{"$lookup": { "from": 'stock_operation', "localField":'_id', "foreignField": 'cid', "as": 'operations_cid'}},
{"$project": {"packets":1, "operations_cid":1} },
{"$addFields": {
"component_reserv": {"$sum": "$operations_cid.reserved"},
"packet_count": {"$sum": "$packets.operations.count"},
"packet_reserv": {"$sum": "$packets.operations.reserv"},
"packet_ordered": {"$sum": "$packets.operations.ordered"},
"packet_price": {
"$function":
{
"body": function(prices, counts) {
let total_counts = Array.sum(counts);
var tmp_count = total_counts;
var total_price = 0;
var c = counts.reverse();
var p = prices.reverse();
for(i in c){
if(c[i] > 0){
if(c[i] < tmp_count){
total_price += (c[i]*p[i]);
tmp_count -= c[i]
}
else{
total_price += (tmp_count*p[i]);
tmp_count = 0;
}
}
}
return total_price;
},
"args": ["$packets.operations.unit_price", "$packets.operations.count"], "lang": "js"
}
}
}
},
{
"$group": {
_id: "$_id",
packets: { $push: "$$ROOT" }
}
},
{"$addFields": {
"component_count": {"$sum": "$packets.packet_count"},
//"component_reserv": {"$first": "$packets.packet_reserv"},
"component_reserv": {"$first": "$packets.component_reserv"},
"component_ordered": {"$sum": "$packets.packet_ordered"},
"component_price": {"$sum": "$packets.packet_price"}
}
}
])
db.category_complete.drop();
db.category.aggregate([
{
"$graphLookup": {
"from": "store_positions",
"startWith": "$parent",
"connectFromField": "parent",
"connectToField": "_id",
"as": "path"
}
},
{
"$set": {
"path": {
"$reverseArray": "$path"
}
}
},
{
"$addFields": {
"path_string": "$path.name"
}
},
{
"$addFields": {
"icon_url": {
"$switch": {
"branches": [
{
"case": {
"$eq": ["$icon_source", "null"]
},
"then": "null"
},
{
"case": {
"$eq": ["$icon_source", "electronic-symbols"]
},
"then": {
"$concat": ["/static/electronic-symbols/SVG/", "$icon", ".svg"]
}
}
],
"default": "null"
}
}
}
}
]).saveAsView("category_complete");