Apologies in advance for what is undoubtetly a silly question.
I'm trying to store the raw JSON documents from MongoDB inside a Node.js array. The following code gives me an atrocity of JSON inside an array, inside a string, inside an array.
let subscriptions = [];
MongoClient.connect(mongourl, function(err, db) {
if (err) throw err;
var dbo = db.db("sigdb");
dbo.collection("customers").find({}).project({ _id: 0 }).toArray(function(err, result) {
if (err) throw err;
subscriptions.push(JSON.stringify(result));
db.close();
});
});
I have tried to exclude toArray()
, using the syntax of findOne()
- no luck. Declaring subscriptions
as a standard variable only returned undefined
. Not putting result
through JSON.stringify()
made the second part of the document appear as [Object]
.
Any suggestions on how to untangle this and just have JSON stored in an array would be much appreciated.
Edit: turns out that instead of subscriptions.push(result)
I could just use subscriptions = result
.