[SERVER-6356] Provide a database stack in shell Created: 09/Jul/12  Updated: 17/Jul/20  Resolved: 17/Jul/20

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Doug Hudson Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Do Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

It's common to jump between a number of databases in the shell. Having to type (even remember) the full name every time can be time consuming. It would be helpful for the shell to maintain a simple stack to jump between databases quickly.

e.g.

// Push another DB onto stack (and change to it)
pushDb('some_db');
// Pop current DB and change to new head.
popDb();
// Switch top two DBs (ala unix shell noarg pushd)
pushDb();
// Bring database at depth n to top of stack
pushDb;
// Show current stack
listDbStack();
// Discard stack
clearDbStack();

There is a related feature request to tab-complete database names in the shell. With these two features, the efficiency of a mongodb shell session could be enhanced.



 Comments   
Comment by Jessica Sigafoos [ 17/Jul/20 ]

This is not a current priority for the new shell.

The new shell can potentially be extended with a user-defined function that does this.

Comment by Doug Hudson [ 05/Sep/12 ]

If anyone ever stumbles across this, I ended up putting together something that adds this functionality to the mongo shell. It overrides (optionally) 'use dbname', and adds pushdb, popdb, dbs, cleardbs, with some other goodies that make my shell experience and db navigation more fluid.

https://github.com/crudson/mongodb-stack

Comment by Doug Hudson [ 09/Jul/12 ]

Sure, but there are other aspects of using a "current" database. We do a lot of collection changes, and "show collections" is probably my most frequently used command. getCollectionNames() is there of course, but having the context of the current db has other advantages, rather than maintaining and referencing pointers to multiple DBs.

I appreciate it's quite a specific request; I will just add some stuff to my .mongorc.js to do this.

Thanks!

Comment by Randolph Tan [ 09/Jul/12 ]

db is actually an object representing the current database selected, so you can store into a variable to make it easy switching between multiple databases. Example:

use some
var someDB = db;
use other
var otherDB = db;
 
someDB.user.insert({s: 1});
otherDB.user.insert({o: 1});
someDB.user.find();
otherDB.user.find();

Generated at Thu Feb 08 03:11:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.