const mongoose = require('mongoose'); 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 sendmsgSchema = new Schema({ idapp: { type: String, }, origin: { userId: { type: String }, page: { type: String }, event_id: { type: String } }, dest: { idapp: { type: String, }, username: { type: String }, }, message: { type: String, }, datemsg: { type: Date, }, read: { type: Boolean, default: false }, deleted: { type: Boolean, default: false }, }); sendmsgSchema.statics.findAllByUserIdAndIdApp = function (userId, username, idapp) { const SendMsg = this; // Filter my msg // // (userId or dest.username === username) and idapp console.log('userId', userId); return SendMsg.find({ $and: [ { $or: [ { 'origin.userId': userId }, { 'dest.username': username }] }, { idapp } ] }, (err, arrmsg) => { // console.log('ris arrmsg:', arrmsg); return arrmsg }); // return SendMsg.find( // { // $and: [ // { // $or: [ // { 'dest.username': username }, // { userId: userId } // ], // }, // { // idapp // } // ] // }, (err, arrmsg) => { // console.log('ris arrmsg:', arrmsg); // return arrmsg // }); }; const SendMsg = mongoose.model('SendMsg', sendmsgSchema); module.exports = { SendMsg };