How to edit multiline queries

How to multipline queries in terminal/cmd? If I miss a bracket or add extra there is no way to go back and rectify the same. In mongo shell it is not possible. If I have to use VSCode then please suggest how to write and run it there. It is really frustrating that every time if there is any error I have to write the entire code from the scratch also the aggregation queries are big so the readability is also getting hampere. Please suggest a way out.

1 Like

Are you using the old mongo shell? You can edit previous commands in editor of your choice right in the shell or you can up-arrow and edit it on the command line (multi-line commands become a single line in the history).

1 Like

Hey Asya,

Yes am using the old mongo shell.

1 Like

If you want an editor-based experience, I’d recommend using Playgrounds in MongoDB for VS Code.

Playgrounds are JS-like environments where you can write scripts in MongoDB-shell syntax and see the results right away in the editor. Documentation is available here: https://docs.mongodb.com/mongodb-vscode/playgrounds/

4 Likes

Add in the extension in VS Code called “Bracket Pair Colorizer 2”, which will help colour your brackets and provide clues to bracket mismatches.

Bracket Pair Colorizer 2

1 Like

What I like to do is to divide my complex queries into simpler snippets using variables. Example, an aggregation with match stage and sort stage:

match_info = { "first_name" : "steeve" , "last_name" : "juneau" }
match_stage = { "$match" : match_info }
sort_info = { "order_date" : 1 }
sort_stage = { "$sort" , sort_info }
pipeline = [ match_stage , sort_stage ]
db.orders.aggregate( pipeline )

The advantages are:

  • I have a very limited number or braces, parenthesis per line
  • When I make a mistake in one variable assignment, it is easy to edit from the command line history and the execute again the other assignments that depends on the modified one.
4 Likes

To manage big pipelines, I would also suggest to use the Aggregation pipeline builder either in MongoDB Compass or directly in MongoDB Atlas if this is an option here.

I then abuse the import / export to code options to manipulate them. It works very well for me.

I think the new Shell (still in beta for now), Mongosh is also trying to solve some of these issues.

Cheers,
Maxime.

2 Likes

For longer aggregations I type them in a notepad (I am on Windows) program and then copy the query into the shell (I go back and forth as needed). Somehow it works for me. For really long entries I use my Programmer’s Notepad 2 (it is my programming code editor) which allows track the braces and formatting. And to read others aggregation queries which are not formatted, I put them in the JSON Formatter!

This is what I generally do but I sometimes it seems very cumbersome. It will be good if we can do it on the fly.
Anyway thanks for the suggestion. :+1:

Yes, I have already install the Mongosh, will definitley try that. Thanks for your suggestions. :+1:

hey, I’m doing that. I would like to ask why the output is not showing in the playground output at the bottom instead it is showing in the right hand side pane named result playgorund in MSVSCode. Any guess? Please let me know. :+1:

In version 0.3.0 onwards the output was redirected from output panel at the bottom to it’s own result window (check the change log for the extension at that point).

The reasons for that are outlined here.

Reason for playground output being redirected.

This is an awesome extension! Great tool for MongoDB inside my favorite editor VSCode.