const mongoose = require('mongoose').set('debug', false) 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 CitySchema = new Schema({ _id: { type: Number, }, istat: { type: String, }, comune: { type: String }, prov: { type: String, maxlength: 3, }, reg: { type: String, maxlength: 3, }, pref: { type: String, }, cap: { type: String, maxlength: 6, }, abitanti: { type: Number, }, country: { type: String, maxlength: 2, }, }); CitySchema.pre('save', async function (next) { if (this.isNew) { const myrec = await City.findOne().limit(1).sort({_id:-1}); if (!!myrec) { if (myrec._doc._id === 0) this._id = 1; else this._id = myrec._doc._id + 1; } else { this._id = 1; } } next(); }); CitySchema.statics.findByCity = function (mycity) { let myregexp = new RegExp(mycity.trim().replace(' ', '|'), 'ig'); const query = [ { $match: {comune: { $regex: myregexp } } }, { $sort: { descr: 1 } } ]; return City .aggregate(query) .then((arrrec) => { return arrrec }) }; CitySchema.statics.getFieldsForSearch = function () { return [{ field: 'comune', type: tools.FieldType.string }, { field: 'prov', type: tools.FieldType.string }, { field: 'reg', type: tools.FieldType.string }, { field: 'pref', type: tools.FieldType.number }, { field: 'cap', type: tools.FieldType.number }, ] }; CitySchema.statics.executeQueryTable = function (idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, 0, params); }; CitySchema.statics.findAllIdApp = async function (idapp) { const myfind = {}; return City.find(myfind); }; const City = mongoose.model('City', CitySchema); module.exports = { City };