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 bookingSchema = new Schema({ idapp: { type: String, }, userId: { type: String, }, id_bookedevent: { type: Number, }, numpeople: { type: Number, }, infoevent: { type: String, }, msgbooking: { type: String, }, datebooked: { type: Date, }, modified: { type: Boolean, default: false }, booked: { type: Boolean, }, }); bookingSchema.statics.findAllByUserIdAndIdApp = function (userId, idapp, sall) { const Booking = this; let myfind = {}; if (sall === '1') myfind = { idapp, booked: true }; else myfind = { userId, idapp, booked: true }; return Booking.find(myfind, (err, arrbooked) => { // console.log('ris Booking:', arrbooked); return arrbooked }); }; function getUsersByBooking(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 } bookingSchema.statics.findAllDistinctByBooking = function (idapp) { const Booking = this; const query = getUsersByBooking(idapp); return Booking.aggregate(query) .then(ris => { return ris }); }; const Booking = mongoose.model('Booking', bookingSchema); module.exports = { Booking };