Shell Output avoid new lines

print(db.files.countDocuments({"refOwnerModel": "teams"}))
print(db.files.countDocuments({"refOwnerModel": "user"}))

I have the problem that in my IntelliShell-Script output after each print command a new emply line is generated.
The output is like:

But it should be like:
Has someone an idea what causes the new empty line?

In addition: These print problems are new, 1 or 2 weeks ago print worked differently and didn’t append an empty line. This new behaviour brakes stuff in a more complex reporting automation that we have, where database script output CSVs are imported into Macro-Excel-Reportings which can not handle empty lines.
At the same time, .count() started throwing deprecated warnings, we now have to switch to countDocuments(). Did you update a mongo/mongosh component to a newer version/syntax?

I would add something to that:

var d = new Date();
print(d.getDate()  + "." + (d.getMonth()+1) + "." + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes())

will output:


21.2.2023 11:18

So why are there 2(!) empty lines between these two prints when a print normally only appends one new line?

So we’ve done some testing on our end and this just seems to be the way that mongosh behaves. This behavior seems to go back as far as 2022.9. We thought about stripping them from the output, but then we’d be running the risk of stripping new lines that are meant to be there, so I’m afraid this is one of those things we’ll have to adapt to.

As far as a possible solution goes, I hate to say it, but it’s probably best to use the legacy shell to avoid the issue, at least until Mongo addresses it - which is not really a solution…I know. We would encourage you to open a ticket with Mongo as well, as the problem can only really be resolved by them.

For some reason, there are many things that just print extra output compared to the legacy shell:

One way to kind of work around this is using an immediately invoked function expression:

image (37)

That way you’d only get one garbage line at the very end. It almost seems like the extra line part of the prompt, but then again, there seem to be some cases when there’s no extra line:

image (38)

We could look into whether we can somehow figure out a reliable way to strip away those empty lines, but the fact that not all commands cause an empty line will make this considerably more difficult and is probably best left to Mongo to workout.

I hope that helps!


That helped a lot and dispite the fact that this isn’t beautiful, this is what I came up with:

(function() {
  var d = new Date();
  print(d.getDate()  + "." + (d.getMonth()+1) + "." + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes())
  print(db.getCollection("accounts").aggregate([ ... ]))

So the code gets wrapped up in this function expression and all aggregates get wrapped up in prints (otherwise it wouldn’t print like it would normally do) and there you go, no empty lines:

3.3.2023 14:54
[ { userId: 11924 } ] 
1 Like