Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!