diff --git a/mongodb/Esempio.mongodb b/mongodb/Esempio.mongodb new file mode 100644 index 0000000..919ee7b --- /dev/null +++ b/mongodb/Esempio.mongodb @@ -0,0 +1,32 @@ +use('test_FreePlanet'); + +let passo1 = [ + +] + +let passo2 = [ + +] + +let passo2b = [ +]; + + +let passo3 = [ +]; + + +let aggregation = []; + +let test = false; + +if (test) { + aggregation = [...aggregation, ...passo4]; +} else { + aggregation = [...aggregation, ...passo1]; + aggregation = [...aggregation, ...passo2]; + aggregation = [...aggregation, ...passo2b]; + aggregation = [...aggregation, ...passo3]; +} + +db.users.aggregate(aggregation); diff --git a/mongodb/Paolo.mongodb b/mongodb/Paolo.mongodb new file mode 100644 index 0000000..4771129 --- /dev/null +++ b/mongodb/Paolo.mongodb @@ -0,0 +1,280 @@ +// MongoDB Playground +// To disable this template go to Settings | MongoDB | Use Default Template For Playground. +// Make sure you are connected to enable completions and to be able to run a playground. +// Use Ctrl+Space inside a snippet or a string literal to trigger completions. + +// Select the database to use. +use('test_FreePlanet'); + + +let passo1 = [ + { + $match: { + idapp: "13", + "profile.mycircuits": { + $elemMatch: { + circuitname: { + $eq: "RISO Viterbo", + }, + }, + }, + }, + }, + { + $lookup: { + from: "circuits", + as: "circuit", + let: { + circuitname: "RISO Viterbo", + idapp: "$idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$name", + "$$circuitname", + ], + }, + { + $eq: [ + "$idapp", + "$$idapp", + ], + }, + ], + }, + }, + }, + ], + }, + }, + { + $unwind: "$circuit", + }, + { + $project: { + username: 1, + profile: 1, + idapp: 1, + "circuit.name": 1, + "circuit._id": 1, + "circuit.refused_users": 1, + }, + }, +] + +let passo2 = [ + { + $lookup: { + from: "accounts", + as: "account", + let: { + username: "$username", + idapp: "$idapp", + circuitId: "$circuit._id", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + { + $eq: [ + "$$circuitId", + "$circuitId", + ], + }, + ], + }, + }, + }, + ], + }, + }, +] + +let passo2b = [ + { + $unwind: "$account", + } +]; + + +let passo3 = [ + { + $match: { + idapp: "13", + }, + }, + { + $sort: { + desc: 1, + }, + }, + +]; + +let passo3b = [ + { + $unwind: "$circuit", + }, +] + +let passo3c = [ + { + $lookup: { + from: "circuit.refused_users", + as: "myris", + let: { + username: "$username", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + ], + }, + }, + }, + ], + }, + }, +] + +let passo3cOFF = [ + { + $match: { + "circuit.refused_users": { + $elemMatch: { + username: { + $eq: "Emilie", + }, + }, + }, + }, + }, + +]; + +let passo4 = [ + { + $lookup: { + from: "users", + localField: "refused_users.username", + foreignField: "username", + as: "user", + }, + }, + { + $project: { + "user.idapp": 1, + "user.username": 1, + "user.profile.img": 1, + "user.profile.qualifica": 1, + }, + }, +] + +let passo4b = [ + { + $replaceRoot: { + newRoot: { + $mergeObjects: [ + { + $arrayElemAt: [ + "$user", + 0, + ], + }, + "$$ROOT", + ], + }, + }, + }, +] + +let passo4c = [ + + { + $unwind: "$user", + }, + { + $replaceRoot: { + newRoot: "$user", + }, + }, +] + +let passo5 = [ + { + $group: { + _id: null, + count: { + $sum: 1, + }, + results: { + $push: "$$ROOT", + }, + }, + }, + { + $project: { + count: 1, + rows: { + $slice: [ + "$results", + 0, + 10, + ], + }, + }, + } +]; + + +let aggregation = []; + +let test = false; + +if (test) { + aggregation = [...aggregation, ...passo4]; +} else { + aggregation = [...aggregation, ...passo1]; + aggregation = [...aggregation, ...passo2]; + aggregation = [...aggregation, ...passo2b]; + aggregation = [...aggregation, ...passo3]; + aggregation = [...aggregation, ...passo3b]; + aggregation = [...aggregation, ...passo3c]; + // aggregation = [...aggregation, ...passo4]; + aggregation = [...aggregation, ...passo4b]; + // aggregation = [...aggregation, ...passo4c]; + // aggregation = [...aggregation, ...passo5]; +} + +// Run the aggregation and open a cursor to the results. +// Use toArray() to exhaust the cursor to return the whole result set. +// You can use hasNext()/next() to iterate through the cursor page by page. +db.users.aggregate(aggregation); diff --git a/mongodb/TabellaMovSchedeOK.mongodb b/mongodb/TabellaMovSchedeOK.mongodb new file mode 100644 index 0000000..2013cc7 --- /dev/null +++ b/mongodb/TabellaMovSchedeOK.mongodb @@ -0,0 +1,199 @@ +use('test_FreePlanet'); + +let passo1 = [ + + { + $sort: { + transactionDate: -1, + }, + }, + { + $match: { + idapp: "13", + $or: [ + { + accountFromId: "632b300002f403d67895d79c", + }, + { + accountToId: "632b300002f403d67895d79c", + }, + ], + }, + }, + { + $lookup: { + from: "accounts", + localField: "accountFromId", + foreignField: "_id", + as: "accfrom", + }, + }, + { + $unwind: "$accfrom", + }, + { + $lookup: { + from: "users", + let: { + username: "$accfrom.username", + idapp: "$accfrom.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userfrom", + }, + }, + { + $unwind: "$userfrom", + }, + { + $lookup: { + from: "accounts", + localField: "accountToId", + foreignField: "_id", + as: "accto", + }, + }, + { + $unwind: "$accto", + }, + { + $lookup: { + from: "circuits", + localField: "accfrom.circuitId", + foreignField: "_id", + as: "circuitfrom", + }, + }, + { + $unwind: "$circuitfrom", + }, + { + $lookup: { + from: "circuits", + localField: "accto.circuitId", + foreignField: "_id", + as: "circuitto", + }, + }, + { + $unwind: "$circuitto", + }, + { + $lookup: { + from: "users", + let: { + username: "$accto.username", + idapp: "$accto.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userto", + }, + }, + { + $unwind: "$userto", + }, + { + $project: { + transactionDate: 1, + amount: 1, + causal: 1, + notifId: 1, + "circuitfrom.symbol": 1, + "circuitto.symbol": 1, + "userfrom.username": 1, + "userfrom.profile.img": 1, + "userto.username": 1, + "userto.profile.img": 1, + }, + }, + { + $group: { + _id: null, + count: { + $sum: 1, + }, + results: { + $push: "$$ROOT", + }, + }, + }, + { + $project: { + count: 1, + rows: { + $slice: [ + "$results", + 0, + 20, + ], + }, + }, + } +]; + +let passo2 = []; + +let passo2b = [ +]; + + +let passo3 = [ +]; + + +let aggregation = []; + +let test = false; + +if (test) { + aggregation = [...aggregation, ...passo4]; +} else { + aggregation = [...aggregation, ...passo1]; + aggregation = [...aggregation, ...passo2]; + aggregation = [...aggregation, ...passo2b]; + aggregation = [...aggregation, ...passo3]; +} + +db.movements.aggregate(aggregation); diff --git a/mongodb/TabellaMovimenti.mongodb b/mongodb/TabellaMovimenti.mongodb new file mode 100644 index 0000000..c687ecd --- /dev/null +++ b/mongodb/TabellaMovimenti.mongodb @@ -0,0 +1,570 @@ +use('test_FreePlanet'); + +let orig = +[ + { + $match: { + idapp: "13", + "profile.mycircuits": { + $elemMatch: { + circuitname: { + $eq: "RISO Test", + }, + }, + }, + }, + }, + { + $lookup: { + from: "circuits", + as: "circuit", + let: { + circuitname: "RISO Test", + idapp: "$idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$name", + "$$circuitname", + ], + }, + { + $eq: [ + "$idapp", + "$$idapp", + ], + }, + ], + }, + }, + }, + ], + }, + }, + { + $unwind: "$circuit", + }, + { + $project: { + username: 1, + profile: 1, + idapp: 1, + "circuit.name": 1, + "circuit._id": 1, + }, + }, + { + $lookup: { + from: "accounts", + as: "account", + let: { + username: "$username", + idapp: "$idapp", + circuitId: "$circuit._id", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + { + $eq: [ + "$$circuitId", + "$circuitId", + ], + }, + ], + }, + }, + }, + ], + }, + }, + { + $unwind: "$account", + }, + { + $sort: { + transactionDate: -1, + }, + }, + { + $match: { + idapp: "13", + $or: [ + { + accountFromId: "632b300002f403d67895d79c", + }, + { + accountToId: "632b300002f403d67895d79c", + }, + ], + }, + }, + { + $lookup: { + from: "accounts", + localField: "accountFromId", + foreignField: "_id", + as: "accfrom", + }, + }, + { + $unwind: "$accfrom", + }, + { + $lookup: { + from: "users", + let: { + username: "$accfrom.username", + idapp: "$accfrom.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userfrom", + }, + }, + { + $unwind: "$userfrom", + }, + { + $lookup: { + from: "accounts", + localField: "accountToId", + foreignField: "_id", + as: "accto", + }, + }, + { + $unwind: "$accto", + }, + { + $lookup: { + from: "circuits", + localField: "accfrom.circuitId", + foreignField: "_id", + as: "circuitfrom", + }, + }, + { + $unwind: "$circuitfrom", + }, + { + $lookup: { + from: "circuits", + localField: "accto.circuitId", + foreignField: "_id", + as: "circuitto", + }, + }, + { + $unwind: "$circuitto", + }, + { + $lookup: { + from: "users", + let: { + username: "$accto.username", + idapp: "$accto.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userto", + }, + }, + { + $unwind: "$userto", + }, + { + $project: { + transactionDate: 1, + amount: 1, + causal: 1, + notifId: 1, + "circuitfrom.symbol": 1, + "circuitto.symbol": 1, + "userfrom.username": 1, + "userfrom.profile.img": 1, + "userto.username": 1, + "userto.profile.img": 1, + }, + }, + { + $group: { + _id: null, + count: { + $sum: 1, + }, + results: { + $push: "$$ROOT", + }, + }, + }, + { + $project: { + count: 1, + rows: { + $slice: [ + "$results", + -20, + 20, + ], + }, + }, + }, +]; + +let passo1 = +[ + { + $match: { + idapp: "13", + "profile.mycircuits": { + $elemMatch: { + circuitname: { + $eq: "RISO Test", + }, + }, + }, + }, + }, + { + $lookup: { + from: "circuits", + as: "circuit", + let: { + circuitname: "RISO Test", + idapp: "$idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$name", + "$$circuitname", + ], + }, + { + $eq: [ + "$idapp", + "$$idapp", + ], + }, + ], + }, + }, + }, + ], + }, + }, + { + $unwind: "$circuit", + }, + { + $project: { + username: 1, + profile: 1, + idapp: 1, + "circuit.name": 1, + "circuit._id": 1, + }, + }, + { + $lookup: { + from: "accounts", + as: "account", + let: { + username: "$username", + idapp: "$idapp", + circuitId: "$circuit._id", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + { + $eq: [ + "$$circuitId", + "$circuitId", + ], + }, + ], + }, + }, + }, + ], + }, + }, + { + $unwind: "$account", + }, + +] +let passo2 = [ + { + $sort: { + transactionDate: -1, + }, + }, + { + $match: { + idapp: "13", + $or: [ + { + accountFromId: "632b300002f403d67895d79c", + }, + { + accountToId: "632b300002f403d67895d79c", + }, + ], + }, + }, + { + $lookup: { + from: "accounts", + localField: "accountFromId", + foreignField: "_id", + as: "accfrom", + }, + }, + { + $unwind: "$accfrom", + }, + { + $lookup: { + from: "users", + let: { + username: "$accfrom.username", + idapp: "$accfrom.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userfrom", + }, + }, + { + $unwind: "$userfrom", + }, + { + $lookup: { + from: "accounts", + localField: "accountToId", + foreignField: "_id", + as: "accto", + }, + }, + { + $unwind: "$accto", + }, + { + $lookup: { + from: "circuits", + localField: "accfrom.circuitId", + foreignField: "_id", + as: "circuitfrom", + }, + }, + { + $unwind: "$circuitfrom", + }, + { + $lookup: { + from: "circuits", + localField: "accto.circuitId", + foreignField: "_id", + as: "circuitto", + }, + }, + { + $unwind: "$circuitto", + }, + { + $lookup: { + from: "users", + let: { + username: "$accto.username", + idapp: "$accto.idapp", + }, + pipeline: [ + { + $match: { + $expr: { + $and: [ + { + $eq: [ + "$$username", + "$username", + ], + }, + { + $eq: [ + "$$idapp", + "$idapp", + ], + }, + ], + }, + }, + }, + ], + as: "userto", + }, + }, + { + $unwind: "$userto", + }, + { + $project: { + transactionDate: 1, + amount: 1, + causal: 1, + notifId: 1, + "circuitfrom.symbol": 1, + "circuitto.symbol": 1, + "userfrom.username": 1, + "userfrom.profile.img": 1, + "userto.username": 1, + "userto.profile.img": 1, + }, + }, + { + $group: { + _id: null, + count: { + $sum: 1, + }, + results: { + $push: "$$ROOT", + }, + }, + }, + { + $project: { + count: 1, + rows: { + $slice: [ + "$results", + -20, + 20, + ], + }, + }, + }, +] +; + +let passo2b = [ +]; + + +let passo3 = [ +]; + + +let aggregation = []; + +let test = false; + +if (test) { + aggregation = [...aggregation, ...passo4]; +} else { + // aggregation = [...aggregation, ...passo1]; + aggregation = [...aggregation, ...passo2]; + aggregation = [...aggregation, ...passo2b]; + aggregation = [...aggregation, ...passo3]; +} + +db.movements.aggregate(aggregation); diff --git a/src/server/tools/general.js b/src/server/tools/general.js index ef40e7e..d8ada63 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -1687,7 +1687,7 @@ module.exports = { throw new Error('endRow must be number'); } - let newvers = !!params.lookup1; + let newvers = !!params.newvers; let query = []; @@ -1924,6 +1924,7 @@ module.exports = { }); if (qa1) query = [...query, ...qa1]; query.push({$unwind: '$user'}); + query.push({ $match: { $and: [ @@ -1931,6 +1932,7 @@ module.exports = { ], }, }); + query.push({ $replaceRoot: { @@ -1942,9 +1944,11 @@ module.exports = { } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_USER_GRP || params.querytype === shared_consts.QUERYTYPE_REFUSED_USER_CIRCUIT) { + /* const myq = this.addQueryIdMatch(params); if (myq) query.push(myq); + */ query.push({$project: {refused_users: 1}}); const qa1 = this.getLookup( @@ -1961,6 +1965,7 @@ module.exports = { }); if (qa1) query = [...query, ...qa1]; query.push({$unwind: '$user'}); + /* query.push({ $match: { $and: [ @@ -1968,6 +1973,7 @@ module.exports = { ], }, }); + */ query.push({ $replaceRoot: { @@ -1994,6 +2000,11 @@ module.exports = { } + if (params.filterextra2) { + if (params.filterextra2.length > 0) + query = [...query, ...params.filterextra2]; + } + if (newvers) { // NUOVA VERSIONE let proj = params.lookup2 ? params.lookup2.lk_proj : null;