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"}