const mongoose = require('mongoose').set('debug', false) const Schema = mongoose.Schema; mongoose.Promise = global.Promise; mongoose.level = "F"; const { ObjectID } = require('mongodb'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const sendNotifSchema = new Schema({ idapp: { type: String, }, type: { type: Number, }, sender: { // mittente type: String, }, dest: { type: String, }, descr: { type: String, }, datenotif: { type: Date, }, status: { type: Number, }, read: { type: Boolean, default: false }, deleted: { type: Boolean, default: false }, }); sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function (username, lastdataread, idapp) { const SendNotif = this; return SendNotif.find({ $and: [ { idapp }, { 'dest': username }, { 'datenotif': {$gt: new Date(lastdataread)} }, ] }).then((arrnotif) => { console.log('arrnotif', arrnotif.length); return arrnotif }).catch((err) => { console.error('err', err); }); }; sendNotifSchema.statics.findLastGroupByUserIdAndIdApp = function (username, idapp) { const SendNotif = this; return SendNotif.aggregate([ { $match: { idapp, dest: username, } }, { $group: { _id: "$dest", descr: { $last: "$message" }, datenotif: { $last: "$datenotif" }, read: { $last: "$read" } } }, { $sort: { datenotif: -1 } }, ]) .then((arrnotif) => { // Remove duplicate // Exclude my chat const myarr = arrnotif.filter((ris) => ris._id !== username); // console.table(myarr); return myarr }).catch((err) => { console.error(err); }); }; const SendNotif = mongoose.model('SendNotif', sendNotifSchema); module.exports = { SendNotif: SendNotif };