Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda