Home > database >  what is printjsononline in mongosh and how to replace it?
what is printjsononline in mongosh and how to replace it?

Time:08-25

In mongosh

% mongosh
Current Mongosh Log ID: 630639411fcf560da1e8d627
Connecting to:      mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh 1.5.4
Using MongoDB:      6.0.1
Using Mongosh:      1.5.4

I am getting the error

test> printjsononline({teste : 1})
ReferenceError: printjsononline is not defined

But it used to work in mongo tool, before it was upgraded to mongosh. What did that function? How to replace it? This is used by an emacs mode (http://github.com/own-pt/sensetion.el) that I need to use.

CodePudding user response:

Most functions which are removed in new mongosh are provided by the mongosh-snippets package in Github.

load('mongonative.js');
load('mongoassert.js');
load('mongotypes.js');

Or write your own function:

if (typeof tojsononeline == 'undefined') {
   function tojsononeline(x) {
      return EJSON.stringify(x)
         .replace(/\{"\$date":"(.{19,23}Z)"\}/g, "ISODate(\"$1\")")
         .replace(/\{"\$oid":"(\w{24})"\}/g, "ObjectId(\"$1\")")
         .replace(/\{"\$numberDecimal":"(. ?)"\}/g, "Decimal128(\"$1\")");
   }
}

CodePudding user response:

As mentioned in the Mongosh documentation:

mongosh supports a subset of the mongo shell methods. 
Achieving feature parity between mongosh and the mongo shell is an ongoing 
effort.

I don't think the support for printjsononline is added yet. However, you can try printjson method of mongosh, if it works the same.

Check this list to find all the methods supported by mongosh.

  • Related