How to convert mongodb response array from javascript object to JSON string

I wrote a javascript API which, upon request, returns all the data from the mongodb database. However, it sends data to an array of objects, and I want to get a simple json string. Operator returning objects

return db.collection('variants').find().toArray(); 

Do I need to add another function like JSON.stringify ()? but I think the work is for a single object, but not for an array of objects, as in my case.

 var fetch = require('graphql-fetch'); const API_URL = `http://localhost:4000/graphql` const query = ` { variants{ VARIANT_ID CHROM } } ` fetch(API_URL)(query).then(data => console.log(data)) 

enter image description here

+7
json javascript mongodb bson
source share
3 answers

Ok, I found a solution. All I need is JSON.stringify(data) .

 var fetch = require('graphql-fetch'); const API_URL = `http://localhost:4000/graphql` const query = ` { variants{ VARIANT_ID CHROM } } ` fetch(API_URL)(query).then(data => console.log(JSON.stringify(data))) 
+4
source share

The following snippet will work correctly.

 fetch('/users.json') .then(function(response) { return response.json() }).then(function(json) { console.log('parsed json', json) }).catch(function(ex) { console.log('parsing failed', ex) }) 
0
source share

You can use mongoexport.

To perform this operation, you need read access to the database.

For example: mongoexport --db database [--collection traffic] --out file.json

-3
source share

All Articles