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

128
Vistas
How to convert database results in Mongo shell to JavaScript object

I have a basic query that returns two matching documents. I want to convert the results into JavaScript objects.

var results = db.dates.aggregate( [
{ $match: { $or: [ { name: { $eq: "name one"} }, { name: { $eq: "name two"} } ] } },
{ $project: { 
    _id: 0, 
    date: 1,
    name: 1
}} // end project 
]);

// to JSON object
var dates = JSON.parse(JSON.stringify("[" + results + "]"));
print(dates[0].date);

The mongo shell outputs "[unknown type]"

However, the browser console converts the string to an object successfully when I use the same methods and manually copy the query results

var dates = JSON.parse(JSON.stringify("[" + string + "]"));

Why is the monggo shell not converting results to an object even though the browser does?

btw, I can only use the mongo shell with plain JavaScript and nothing else. Mongo version is 4.2

The output of the query is:

{"name":"name one","date":"2022-01-08"}
{"name":"name two","date":"2016-11-10"}
about 4 years ago · Juan Pablo Isaza
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