I have a collection called "inventory_devices" and from this I need to get the documents that are close to maintenance (usually 30 days before the next maintenance date) and at the moment I am trying to filter the documents that are in the field "date_close_to_maintenance" 30 days before the date and I can't find the way to do it. "maintenance_next_date" is String type. I currently use the Mongoose library for MongoDB queries in NodeJS.
The variable/field "contract" is the common identifier that the documents have and it is the one that serves me to group them. Each X number of documents have a contract associated, something like a "Customer type" of a customer table.
The model has the following, I will show the data that are used only because the model could take 300 rows:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const InventarioDispositivosSchema = new mongoose.Schema({
contrato: {
type: Schema.Types.ObjectId,
ref: 'Contrato'
},
informacion_equipo: {
type: Object,
require: ["sistema_operativo", "ip", "nombre_equipo", "usuario_red"],
properties: {
ususario_red: { //Este campo y el modelo en sí funciona como está
type: String,
required: false,
trim: true,
},
},
},fecha_proximo_mantenimiento: {
type: String,
required: false,
trim: true,
}, { collection: 'inventario_dispositivos', timestamps: true })
const InventarioDispositivos = mongoose.model('InventarioDispositivos', InventarioDispositivosSchema)
module.exports = InventarioDispositivos
I have the following query:
// With "contrato" burned
let contrato = 'contrato1';
Dispositivos.find(
{
//Filtros que se le hacen a la consulta
contrato,
fecha_proximo_mantenimiento: {
$exists: true
}
},
{
//Datos que quiero que se muestren, solo el ID se oculta.
_id: 0,
'informacion_equipo.ususario_red': 1,
fecha_proximo_mantenimiento: 1
}
).limit(10).then((devices) => {
console.log(devices)
}).catch(err => {
console.log(err)
})
A sample of the model shown and what the above query returns:
{
informacion_equipo: { ususario_red: 'angel.goez' },
fecha_proximo_mantenimiento: '2021-08-30'
},
{
informacion_equipo: { ususario_red: 'angel.goez' },
fecha_proximo_mantenimiento: '2021-08-25'
}