-1
{ "inprog" : [ { "host" : "DESKTOP-LTB3QQ5:27017", "desc" : "conn25", "connectionId" : 25, "client" : "127.0.0.1:38354", "appName" : "MongoDB Shell", "clientMetadata" : { "application" : { "name" : "MongoDB Shell" }, "driver" : { "name" : "MongoDB Internal Client", "version" : "3.6.8" }, "os" : { "type" : "Linux", "name" : "Ubuntu", "architecture" : "x86_64", "version" : "20.04" } }, "active" : true, "currentOpTime" : "2021-03-16T11:01:14.599+0530", "opid" : 3135, "lsid" : { "id" : UUID("82088a94-16c2-4aa2-92d8-c557e74049a1"), "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=") }, "secs_running" : NumberLong(0), "microsecs_running" : NumberLong(65), "op" : "command", "ns" : "admin.$cmd.aggregate", "command" : { "currentOp" : 1, "lsid" : { "id" : UUID("82088a94-16c2-4aa2-92d8-c557e74049a1") }, "$db" : "admin" }, "numYields" : 0, "locks" : { }, "waitingForLock" : false, "lockStats" : { } } ], "ok" : 1 }

command cat mon.json | jq

when I parse this with jq I get this error

parse error: Invalid numeric literal at line 1, column 493

I think this is because "id": UUID("82088a94-16c2-4aa2-92d8-c557e74049a1")

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
DB guy
  • 25
  • 1
  • 2
  • 6
  • As the message says, that's not valid JSON. `UUID(...)`, `BinData(...)`, `NumberLong(...)`, etc. are invalid. – John Kugelman Mar 16 '21 at 06:18
  • The fragments `BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")` and `NumberLong(...)` are also not valid JSON; also your line numbers etc do not match up. Going forward, see the guidance for providing a [mre]. – tripleee Mar 16 '21 at 06:18
  • Also, tangentially, [the `cat` is useless.](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Mar 16 '21 at 06:19
  • 2
    To answer your actual question, there is no way for a JSON parser to parse text which is not JSON. Probably request corrected output from whoever or whatever provided you with this data. – tripleee Mar 16 '21 at 06:21
  • 1
    You would have to convert your data to JSON before being able to use jq with it. See https://stackoverflow.com/questions/38144236/how-to-convert-a-mongodb-document-to-json-object – peak Mar 16 '21 at 06:21

2 Answers2

2

The logfile is in BSON format, however jq parses only JSON, see JSON and BSON.

Obviously your files comes from db.currentOp(), see https://dba.stackexchange.com/questions/287020/save-output-of-a-mongo-command-to-a-variable-bash-script

Why do you try to run a shell script? You an do it all in mongo shell script (which is JavaScript). Could look similar to this:

db.currentOp().inprog.forEach(function (x) {
   if (x.op == "find" && x.secs_running > 5) {
      db.killOp(x.opid);
   }
})

Printing all commands would be similar to this:

db.currentOp().inprog.map(x => x.command)

I would say, it's time to learn JavaScript.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • Yes, I would rather do it in javascript or python, but I am required to do it in shell script – DB guy Mar 16 '21 at 07:01
  • That's really pointless. Use the shell script only to call the javascript, i.e. `mongo -u username -p password --quiet ` or use the `--eval` option, i.e. `mongo -u username -p password --quiet --eval "db.currentOp().inprog.map(x => x.command)"`. It's no problem to put multiple commands into `eval` option. – Wernfried Domscheit Mar 16 '21 at 07:09
-1

If you want to use jq with your data, you will first have to convert it to (or render it as) JSON. This can be done in various ways, e.g. using mongoexport:

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance.

https://docs.mongodb.com/database-tools/mongoexport/

peak
  • 105,803
  • 17
  • 152
  • 177