const mongoose = require('mongoose').set('debug', false) const Schema = mongoose.Schema; const tools = require('../tools/general'); const { ObjectId } = require('mongodb'); mongoose.Promise = global.Promise; mongoose.level = "F"; // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const Foto = { imagefile: { type: String, }, alt: { type: String, }, description: { type: String, }, }; const FilesCataloghi = { per_web: { type: String, }, per_stampa: { type: String, }, }; const CatalogSchema = new Schema({ idapp: { type: String, }, active: { type: Boolean, default: false, }, title: { type: String, }, foto_collana: Foto, idCollane: [{ type: Number, }], descr_introduttiva: { type: String, }, idPageAssigned: { type: String, }, referenti: [{ type: String, }], img_bordata_web: Foto, img_bordata_stampa: Foto, img_intro_web: Foto, img_intro_stampa: Foto, generati: FilesCataloghi, online: FilesCataloghi, date_created: { type: Date, default: Date.now }, date_updated: { type: Date, }, }); CatalogSchema.pre('save', async function (next) { if (this.isNew) { this._id = new ObjectId(); } next(); }); CatalogSchema.statics.getFieldsForSearch = function () { return [{ field: 'title', type: tools.FieldType.string }] }; CatalogSchema.statics.executeQueryTable = function (idapp, params, user) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params, user); }; CatalogSchema.statics.findAllIdApp = async function (idapp) { const Catalog = this; const arrrec = await Catalog.aggregate([ // Filtra i documenti per idapp { $match: { idapp } }, // Ordina i risultati per titolo { $sort: { title: 1 } }, // Esegui il join con la collezione Collana { $lookup: { from: "collanas", // Nome della collezione Collana localField: "idCollane", // Campo in Catalog foreignField: "idCollana", // Campo in Collana as: "collana_info" // Nome del campo che conterrĂ  i risultati del join } }, ]); return arrrec; }; const Catalog = mongoose.model('Catalog', CatalogSchema); Catalog.createIndexes((err) => { if (err) throw err; }); module.exports = { Catalog };