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

235
Vistas
Executing same query gives "FailedToParse" error in nodejs

Database structure is;

{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }
{ "key" : "test" }

In mongoshell the query,

db.collectionName.find({}, {key: 1, _id: 0})

lists the documents.

However, when I run the same query in nodejs it gives me an error as;

{
  "name": "MongoError",
  "message": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "ok": 0,
  "errmsg": "Failed to parse: comment: 1. 'key' field must be of BSON type string.",
  "code": 9,
  "codeName": "FailedToParse"
}

I don't understand what can be the problem?

I am aware that the question doesn't make sense, however,meaninglessly this is what I have. Is it about mongo versions or what?

edit: You are right about writing Node.js code, so here it is, the query inside an async.series call;

async.series([
  function(callback){
    db.collection("collectionName").find({}, {key: 1, _id: 0}).toArray(function(err, result){
        if(err){
            callback(err);
        } else{
            callback(null, result);
        }
    });
   }
])
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I don't know why you did include the mongo shell line that worked but didn't include the Node line that failed - which would be much more relevant here.

In any case, the native Mongo driver for Node (which I assume you're using here) works in a completely different way than the Mongo shell so don't expect it to work the same.

Mongo shell executes everything sequentially. In Node you have to use callbacks or promises for flow control which you don't do in the Mongo shell. Also there are some differences in how you access the collections. In the Mongo shell you usually use db.collectionName. Also in the Mongo shell you have some things predefined, like functions related to object ids for example. And finally Mongo uses BSON for data and in Node you use JavaScript object literals (more like JSON but less strict). Node uses V8 plus additional functionality and Mongo shell uses SpiderMonkey, with no functionality that is available in Node. It all have consequences in subtle differences.

You didn't include the Node line that failed so no one will be able to fix that line. But you certainly need to take a look at it and see if you're following the documentation of whetever Mongo driver or ODM you're using.

over 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