Mongosh splits strings with "\n" into invalid value

I’ve tried searching around for this but am not sure how to word it, exactly. For some reason, when retrieving a document using the mongosh interface, any strings containing “\n” are split into “string before \n” + “string after \n”. Example below:

Using legacy mongod connection (or viewing in Compass):

"leadership": [
                                {
                                    "text": "Build trust and inspire my leader to deliver great\nresults and have fun too. ",
                                    "value": "Build trust and inspire my leader to deliver great\nresults and have fun too. ",

Using mongosh:

leadership: [
                  {
                    text: 'Build trust and inspire my leader to set clear goals to deliver great\n' +
                      'results and have fun too. ',
                    value: 'Build trust and inspire my leader to set clear goals to deliver great\n' +
                      'results and have fun too. ',

I really like the improvements done on mongosh, but it’s so frustrating having to redo my whole workstream because aggregation and query results aren’t parsed JSON anymore and there are all these weird quirks to it. I’ve seen some stuff online but are there no official ways to use legacy formatting or functionality?

Appreciate any help on this.

Please note that aside from that problem, the output is not true JSON anyways, e.g. ObjectIds show up as e.g. ObjectId("636c039c124f7c960b99b610").

The best way to get true JSON is to use EJSON.stringify(data). Would that work for you?

1 Like

Yes, I normally exclude the _id for this reason. I did try using EJSON.stringify(data) but I always get this error:

Uncaught:
BSONTypeError: Converting circular structure to EJSON:
    (root) -> _mongo -> __serviceProvider -> mongoClient -> s -> sessionPool -> client
                                                  \-------------------------------/

Or for JSON.stringify(data)

Uncaught:
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'MongoClient'
    |     property 's' -> object with constructor 'Object'
    |     property 'sessionPool' -> object with constructor 'ServerSessionPool'
    --- property 'client' closes the circle

Can you share your entire script? I suspect you might be trying to stringify the cursor return by a find or an aggregate instead of the array of results.

2 Likes

Ah, okay that explains it. So the scripts I was trying were:

var data = db.coll.aggregate([pipeline])
EJSON.stringify(data)

var alt = db.coll.find({ })
EJSON.stringify(alt)

I’ve saved that in a file and am referencing it in a .bat file like so:
mongosh "URI" --quiet < aggregation_pipeline.mongodb | mongoimport [external server details]

After you said this I tried adding .toArray() at the end of the pipeline and that seemed to work. So the contents of aggregation_pipeline.mongodb are now:
EJSON.stringify(db.coll.aggregate([pipeline]))

Thanks, @Massimiliano_Marcon !

1 Like

P.S. For anyone else trying this very silly way of migrating data from one MongoDB server to another within Windows, here are the other things I had to change to make this work:

  • The .mongodb file has to be all on one line or you’ll get a lot of “… …” in the start of your out file
  • The config file at C:\Users\[your user]\AppData\Roaming\mongodb\mongosh needs to have these properties:
    • "disableGreetingMessage":true,"displayBatchSize":X,"inspectDepth":Y
    • where X is the number of documents you’re expecting and Y is the level of sub-document you need (more details in the documentation here)
  • To avoid having the prompt/cursor, you’ll need to add a .mongoshrc.js file to C:\Users\[your user] with this line:
    • prompt = ''

You could also try using mongoexport --fields="myFields" | mongoimport if you don’t need to alter the dataset beyond pulling specific fields

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.