0

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

asked Apr 27, 2022 at 9:18
1
  • What do you mean? db.currentOp() returns a JSON (or BSON being more accurate), what is your problem? Commented Apr 27, 2022 at 15:24

1 Answer 1

0

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月06日T10:51:23.454Z")
test> EJSON.stringify(dt)
{"$date":"2022年05月06日T10: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.

answered May 6, 2022 at 11:02
2
  • ok that works ... but only on mongo5x Commented Jun 6, 2022 at 8:10
  • My example was using mongosh (aka The New MongoDB Shell) which supports MongoDB 4.0 or newer. Commented Jun 17, 2022 at 6:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.