Mongo Query is not working when I have a compound property (one inside the other)

Asked

Viewed 67 times

0

I am trying to perform a query in the database of Mongo, when I have a simple property as for example:

Nome: João

Works smoothly

But if I have a compound property as an example:

Tamanho: { PP: 2 M: 4 G: 10 }

The query does not return any value, I’m trying to deploy a query to return all values that are PP and less than 10 for example, but, different from what happens in a simple query, I can’t get success

Below follows the code

router.get('/produtos', function (req, res) {

    _produto.paginate(
        { quantidade: { PP: { $lt: 10 } } },
        { limit: 21, page: req.query.pag, sort: { nome: 'asc' } },

        (err, res) => {
            if (!res.page) {
                res.page = 1
                res.nextPage = 2
                res.hasPrevPage = false
                res.hasNextPage = true
            }
            return res

        }).then(function (produto) {
            res.render('produtos', { produto: produto.docs, pag: produto })
        })
})

Below follows the schema, the query works for all simple properties, only for composts that are in trouble, probably a fault of mine

const mongoose = require('mongoose')
const mongoosePaginate = require('mongoose-paginate-v2')
const Schema = mongoose.Schema

const Produto = new Schema({
    nome: {
        type: String,
        required: true
    },
    marca: {
        type: String,
        required: true
    },
    slug: {
        type: String,
        required: true
    },
    categoria: {
        type: String,
        required: true
    },
    quantidade: {
        PP: {
            type: Number,
            required: false
        },
        P: {
            type: Number,
            required: false
        },
        M: {
            type: Number,
            required: false
        },
        G: {
            type: Number,
            required: false
        },
        GG: {
            type: Number,
            required: false
        },
        _1: {
            type: Number,
            required: false
        },
        _2: {
            type: Number,
            required: false
        },
        _3: {
            type: Number,
            required: false
        },
        _4: {
            type: Number,
            required: false
        },
        _5: {
            type: Number,
            required: false
        },
        _6: {
            type: Number,
            required: false
        },
        _7: {
            type: Number,
            required: false
        },
        _8: {
            type: Number,
            required: false
        },
        _9: {
            type: Number,
            required: false
        },
        _10: {
            type: Number,
            required: false
        },
        _12: {
            type: Number,
            required: false
        },
        _14: {
            type: Number,
            required: false
        },
        _16: {
            type: Number,
            required: false,
        }
    },
    estacao: {
        type: String,
        required: true
    },
    descricao: {
        type: String,
        required: true
    },
    preco: {
        type: Number,
        required: true
    },
    genero: {
        type: String,
        required: true
    },
    cor: {
        type: String,
        required: true
    },
    desconto: {
        type: Number,
        required: true
    },
    largura: {
        type: Number,
        required: true
    },
    altura: {
        type: Number,
        required: true
    },
    comprimento: {
        type: Number,
        required: true,
    },
    peso: {
        type: Number,
        required: true
    },
    fotos: {
        foto1: {
            type: String,
            required: false
        },
        foto2: {
            type: String,
            required: false
        },
        foto3: {
            type: String,
            required: false
        },
        foto4: {
            type: String,
            required: false
        },
        foto5: {
            type: String,
            required: false
        }
    },
    cadastro: {
        type: Date,
        default: Date.now()
    }
})

Produto.plugin(mongoosePaginate)

mongoose.model('produto', Produto)

I’m using Mongoose next to Mongoose-paginate

  • 1

    May I ask you, you need to schema, everything indicates that it needs relationships, and I doubt that it needs monster scale, because it preferred to use Mongodb and not a relational database that seems more suitable? Probably your difficulties are being using the wrong tool.

1 answer

0


The solution to the problem is simpler than I thought To access a property inside another, just pass its name as string, and with that, run the query as you want.

Instead of:

{ quantidade: { PP: { $lt: 10 } } }

Should use:

{ 'quantidade.PP': { $lt: 10 } }

So the code stays:

router.get('/produtos', function (req, res) {

    _produto.paginate(
        { 'quantidade.PP': { $lt: 10 } },
        { limit: 21, page: req.query.pag, sort: { nome: 'asc' } },

        (err, res) => {
            if (!res.page) {
                res.page = 1
                res.nextPage = 2
                res.hasPrevPage = false
                res.hasNextPage = true
            }
            return res

        }).then(function (produto) {
            res.render('produtos', { produto: produto.docs, pag: produto })
        })
})

I hope this solution will help someone in the future

Browser other questions tagged

You are not signed in. Login or sign up in order to post.