const mongoose = require('mongoose'); const Schema = mongoose.Schema; mongoose.Promise = global.Promise; mongoose.level = "F"; const tools = require('../tools/general'); const { ObjectID } = require('mongodb'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const MyEventSchema = new Schema({ idapp: { type: String, }, typol: { type: String, }, short_tit: { type: String, }, title: { type: String, }, details: { type: String, }, time: { type: String, }, dur: { type: Number, }, dur2: { type: Number, }, days: { type: Number, }, date: { type: Date, }, bgcolor: { type: String, }, icon: { type: String, }, img_small: { type: String, }, img: { type: String, }, where: { type: String, }, contribtype: { // TABLE type: Number, }, price: { type: Number, }, infoafterprice: { type: String, }, teacher: { // TABLE ?! type: String, }, teacher2: { type: String, }, infoextra: { type: String, }, linkpage: { type: String, }, linkpdf: { type: String, }, nobookable: { type: Boolean, }, news: { type: Boolean, }, canceled: { type: Boolean, }, deleted: { type: Boolean, }, dupId: { type: mongoose.Schema.Types.ObjectId, }, modified: { type: Boolean, }, }); MyEventSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) { const Event = this; let myfind = {}; if (sall === '1') myfind = { idapp, booked: true }; else myfind = { userId, idapp, booked: true }; return Event.find(myfind, (err, arrbooked) => { // console.log('ris MyEvent:', arrbooked); return arrbooked }); }; function getUsersByEvent(idapp) { const query = [ { $match: { idapp } }, { $group: { _id: "$userId" } }, { $project: { _id: { $toString: "$userId" } } }, { $Lookup: { from: "users", localField: "userId", // field in my collection foreignField: "ObjectId(_id)", // field in the 'from' collection as: "fromItems" } }, { $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ["$fromItems", 0] },] } } }, { $project: { username: 1, name: 1, surname: 1 } } ]; return query } MyEventSchema.statics.findAllDistinctByEvent = function (idapp) { const Event = this; const query = getUsersByEvent(idapp); return Event.aggregate(query) .then(ris => { return ris }); }; MyEventSchema.statics.executeQueryTable = function (idapp, params) { return tools.executeQueryTable(this, idapp, params); }; const MyEvent = mongoose.model('MyEvent', MyEventSchema); module.exports = { MyEvent };