how to convert mongodb db.currentOp() into json

All we need is an easy explanation of the problem, so here it is.

I want to create an interface for all running processes in Mongodb

How can I convert mongodb db.currentOp() output into json so that I can access that

The output currently is a BSON string and I cannot use any libraries that use json … which would become convenient to develop an interface

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I suspect the problem you are referring to is results returned in a MongoDB shell environment which use JavaScript helper syntax like ISODate() that is not compatible with JSON.

If so, the solution is to convert shell output into MongoDB Extended JSON which has JSON-compatible representations of all BSON data types.

For example, using mongosh:

test> dt = new Date()
ISODate("2022-05-06T10:51:23.454Z")

test> EJSON.stringify(dt)
{"$date":"2022-05-06T10:51:23.454Z"}

If you want to convert db.currentOp() output into Extended JSON for use with standard libraries:

mongosh –eval "EJSON.stringify(db.currentOp())" –quiet

The --quiet option suppresses startup messages (for example, version info or startup warnings) so output should only include results of the --eval call.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply