Execute multiline commands in mongo shell

Hi,

As the pipeline/aggregation queries are quite long. I want them to be readable and execute as multiline query.
for example:

I want the above query to execute in multiline as well.

Is it possible?
Thanks in advance.

Yes. But you should have try it. There is no danger of breaking anything when running a read only command.

When a command is not complete, mongo shell will change the prompt to … to indicate that what you enter is not complete yet.

In addition to @steevej-1495, another way is to use a backslash (\)

db.collection.find({ \
<query> \
})
1 Like

thanks, I tried but not understood the meaning of …, now got it… :wink:

this doesn’t seem to work.
thanks anyway

Hi @Mohit_Singh_30060,

To improve readability of your aggregation pipeline, you can try building it in the text editor of your choice as it will also help you in case you have missed any brackets.
Otherwise you can also try building your aggregation pipeline in MongoDB Compass, it will help you in understanding the output of each stage of the pipeline and you will be able to visualize your pipeline better.

Also, I have edited your above post as it contains the potential hint for the lab.

Please feel free to reach out if you have any questions.

Thanks,
Sonali

Hi there, I’m also trying to write queries in the shell on multiple lines. I can write it, but the issue is that once I change line I cannot browse the previous lines in case I want to correct an error. Is there a way to do it?

Hi @Gianluca_Quercini,

Once you move to the next line, there is no way to edit previous lines.
I would recommend using a text editor to write the pipelines and then copy-pasting it in the mongo shell.

Please feel free to reach out if you have any questions.

Kind Regards,
Sonali

One thing that I do is to split my long queries into a group of smaller one line steps using variables.

For example, rather than writing:

db.test.find( { field_a : 1 , field_b : 2 , field_c : 3 , field_d : 4 } )

I write

a = { field_a : 1 }
b = { field_b : 2 }
c = { field_c : 3 }
d = { field_d : 4 }
q = Object.assign( a , b , c , d )
db.test.find( q )

I do have more initial typing but when, not if, I make errors I can use the command line history and only do the correction on the variable with the error, redo the q = Object.assign(…) using the command line history without typing it again.

Same can be done with aggregations. Rather than

db.collection.aggregate( [ { $match : { a_long_query_specification } } ,
... { $group : { a_long_group_specification } } ,
... { $project : { a_long_project_specification } } ] )

I write:

c = db.collection
m_spec = { a_long_match_specification }
m = { "$match" : { m_spec } }
g_spec = { a_long_group_specification }
g = { "$group" : { g_spec } }
p_spec = { a_long_project_specification }
p = { "$project" : { p_spec } }
pipeline = [ m , g , p ]
c.aggregate( pipeline )

Same thing, if I make a error while defining one of the stage I simply edit the appropriate variable, rerun the lines pipeline = … and c.aggregate( … ).

YES, it is more typing when I, seldom, make no error. I find this especially useful when working on aggregation. I usually start with a pipeline that only has the match stage. Once I am happy with the match, I work on the grouping. Since my match and my grouping are variables I can easily add and remove them from my pipeline.

And it reduce my braces and brackets errors to quasi-none as I only have a few on each line.

Divide And Conquer

1 Like

So I just redid M001 as a refresher, before going onto this course.

To be honest I would recommend for the aggregate pipeline, Visual Studio Code + the MongoDB extension (written by MongoDB) + bracket colouriser 2 extension. All free tools. It gives you intellisense. Also bracket colouriser helps with the extensive use of brackets in MongoDB. Colours each bracket grouping, and draws a line between related brackets.

Connect VSC to your Atlas Cluster or your local MongoDB on your PC where ever you decide to host your test data. Ideally if you have a local and cluster versions running, keep one clean off changes, so you can be confident you haven’t mucked up the data for exams.

When you build your MongoDB playground, comment out your previous queries, as your work through the course, and that way you have the previous versions a few lines away.

Remembering of course you can kick off a MongoDB shell within any database you are connected to, from within VSC…

PS - This isn’t supported, but I do believe it is worth the investment in time.

PPS - The extension by MongoDB is good, and is getting better :slight_smile:

2 Likes

Can you give a suggestion for a text editor that will help match brackets? Ideally, I want to construct queries over multiple lines in the editor, but then copy-paste it as a single line when pasting it into the mongo shell.