MongoDB for VS Code won't change DB

Hello guys,

New guy to MongoDB here. I wanted to ask here instead of giving a review on VS Marketplace:

I found out about the VS Code extension for MongoDB today and it gave me 2 functionalities that I had wanted ever since I started learning Mongo 2 days ago: color coded statements and Intellisense(or auto-complete).

I started trying playgrounds today and to my utter surprise the extension doesn’t seem to be able to do the most basic of tasks: switch databases.

Using use(‘db_name’); AND use db_name: Both show that the db has been switched in the output but any commands that I execute AFTER use seem to be working on test(default db) only.

Even using db(show active db) shows test even after running db just after use db_name

What’s up with the extension? Using cmd(I’m on windows) works fine

1 Like

Is this how you are using playgrounds?

image

By design, playgrounds don’t preserve the state of the previous run. Every run is purposely sandboxed. This means that you need to have a use() call to select the database before you run the other commands. This has the advantage that you can save your playground and come back to it later (or run it against another cluster) and always run against the same database that you have explicitly set.

5 Likes

Hello M.M.,

Yes that was how I was using playgrounds. I thought to use it like mongo.exe cause that was the only way familiar to me. It makes sense now that you tell it. Thank you for this. One more question that I have: is it possible to assign a shortcut key to the (run playground) command?

Yes, it’s possible. In fact, that’s what I did too.

You just open the keyboard shortcuts editor in vs code (Visual Studio Code Key Bindings), search for MongoDB and assign your favorite shortcuts to MongoDB: Run All From Playground and MongoDB: Run Selected Lines From Playground.

The new version we’ll ship next week will have default keybindings (Cmd/Ctrl + Alt + R and Cmd/Ctrl + Alt + S) but you’ll always be able to customize them to your preference.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.