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?
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.
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]))
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: