10

Is there a way to write to STDOUT without a trailing newline from the Mongo shell? I can't seem to find anything other than print() available.

Sim
  • 13,147
  • 9
  • 66
  • 95
  • 1
    The mongo shell doesn't have an alternative output to `print()` as at 2.0.6 but you could [suggest this as an improvement](https://jira.mongodb.org/browse/SERVER). What would be the use case for this? – Stennie Jul 05 '12 at 00:14
  • 1
    Makes sense in the context of your related question on "[reading a line from the console](http://stackoverflow.com/questions/11333248/mongodb-shell-reading-a-line-from-the-console)". The sort of interaction you are after is currently best done using one of the [MongoDB drivers](http://www.mongodb.org/display/DOCS/Drivers). – Stennie Jul 06 '12 at 03:33
  • Indeed. I may just have to do this via the Rails console. However, the Ruby driver is behind in the way in handles timeouts. I've found the Mongo shell to be far more reliable when it comes to long-running queries. – Sim Jul 06 '12 at 04:18
  • 4
    @Stennie A use-case that I just ran into is wanting to spew out a period when something is done (similar to a progress-bar). I don't want to print out a whole line. I'm in the development-phase, and want to keep an eye on how far I get. I understand I'd have to use one of the drivers instead. I'll settled for "xxx out of yyy completed" in the end. – Harry Pehkonen Oct 24 '14 at 13:18

3 Answers3

4

This is related to my SO question on reading a line from the console. Per @Stennie's comment, it is not possible in the current (2.0.6) version of the Mongo shell.

Community
  • 1
  • 1
Sim
  • 13,147
  • 9
  • 66
  • 95
3

There might be ways to work around it. You can accumulate the results in an intermediate variable (could be an array, string or any other data structure), then print the entire thing in a single line. Below example illustrates use of an array to capture values from query results, then array is converted to string with comma as a separator. In my case I'm interested in just the _id field:

var cursor = db.getCollection('<collection name>').find(<your query goes here>)
let values = []
cursor.forEach((doc) => values.push(doc._id))
print(values.join(','))

Depending on how many results you're expecting, not sure if space consumed by the intermediate data structure might overwhelm memory. If that's the case can craft the query to return smaller, subsets of data that when added together comprise the full result set you're going for.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jose Quijada
  • 558
  • 6
  • 13
-3

This is quite old question, however still relevant, so answering.

One can use printjsononeline().

Bhoju
  • 71
  • 4
  • 1
    The function `printjsononeline(str)` appears to have the effect of removing intermediate whitespace from returned JSON documents, but not from suppressing the behavior of advancing the terminal one line after printing - i.e. removing the trailing newline. I believe the latter is what the question was looking for. – WAF May 22 '17 at 19:04