Is the output of MongoDB a JSON object or a document?

The collection in MongoDB can be exported to a JSON file using the mongoexport parameter.


Solution 1:

Here’s the actual answer:

To transform a mongo object into a JSON object, simply utilize the utility method present in every mongo object, as shown in

toJSON

.

By executing

mongoResponseObject.toJSON()

on the response object, you can easily perform the desired action.

e.g.

Products.findById(id).then(res => {
    const jsonRes = res.toJSON();
    // Here jsonRes is JSON
})

You have the option of obtaining the JSON object directly through the utilization of

.lean()

.

Products.findById(id).lean().then(res => {
    // Here res is JSON
})


Solution 2:


To achieve the task, it is necessary to change the object id to a string format.

var result = {
  "_id": ObjectId("5739a6bf3f1b41477570dc89"),
  "taskCount": 2,
  "study": "cod"
};
//now convert to string 
result=result._id.toString();
//now you can use the result


Solution 3:

Try this,

var cursor =db.collection('rhimeReport').find(
       {"study":"cod","phase":"mansa2","recordStudy":"codstudy",
        "recordPhase":"mansa2","_state":"CodingComplete"});
cursor.toString();
......

Hope this help.

Frequently Asked Questions