Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

379
Vistas
How to treat custom MongoDB queries in NodeJS

I'm porting a webserver from python to node.js and one of the capabilities that server must have is to receive a POST containing information about a MongoDB query, and send the result of the query to the webpage. The query may or may not contain a filter, a projection, a limit and a sort. In python we can do it like this:

(In the examples below command_name equals to 'find')

command = getattr(collection, command_name)

cmd_filter = loads(filter_str) if filter_str else None
projection = loads(projection_str) if projection_str else None
limit = loads(limit_str) if limit_str else 0
sort = [(elt['key'], elt['order']) for elt in loads(sort_str)] if sort_str else None

response = command(filter=cmd_filter, projection=projection, sort=sort, limit=limit)

But I'm having trouble finding a way to do it in node.js, I've tried stuff like this:

command_name = req.body['command']
command = collection[command_name]

command = filter_str != undefined ? collection[command_name](JSON.parse(filter_str)) : collection[command_name]
command = projection_str != undefined ? command['project'](JSON.parse(projection_str)) : command
command = sort_str != undefined ? command['sort'](JSON.parse(sort_str)) : command
command = limit_str != undefined ? command['limit'](JSON.parse(limit_str)) : command

response = command.toArray(function(err, result) {
    if (err) throw err

    console.log(result)
})

But it doesn't work. I get a MongoError: Can't canonicalize query: BadValue bad order array [2]

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your code not working is the least of your problems here. I don't know if you realize but a user may execute any command on the database with your API. I don't know if there is such a name but you have a Mongo injection vulnerability. I have never seen anything like that before, explicitly running user provided random methods with random data on a live database.

Search for Mongoose pagination, sorting etc. for REST APIs in Node and you will find a lot of info on how to do it properly.

There are some modules that can help you with that:

  • https://www.npmjs.com/package/mongoose-paginate
  • https://www.npmjs.com/package/mongoose-paginator
  • https://github.com/expressjs/express-paginate
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda