[SERVER-14018] Refactor the "sh" and "rs" objects in the mongo shell Created: 21/May/14  Updated: 06/Dec/22  Resolved: 03/Dec/21

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

Type: Improvement Priority: Major - P3
Reporter: Andy Schwerin Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

The global "rs" and "sh" objects contain a variety of utility methods useful to users working at the shell command prompt, like sh.stopBalancer(). However, these utility methods often rely on the "db" global variable, making them difficult to use in scripts and tests. The "rs" and "sh" objects' methods should be refactored to delegate to methods that take the "db" object as an explicit parameter, and then tests and scripts could use those methods instead. This would eliminate the need for error-prone code like the following in tests and scripts:

  var oldDb = db;
  try {
    db = // some other Database object
    sh.stopBalancer()
  }
  finally {
    db = oldDb;
  }



 Comments   
Comment by Kevin Pulo [ 22/Aug/18 ]

I've also never been a fan of the design of passing string namespaces to the sh functions, eg. sh.shardCollection(string_namespace, ...), to the point that I wrote wrappers to instead let me do db.enableSharding(), db.foo.shardCollection(...), etc.

Perhaps simplest would be to just migrate these functions so that they live inside the DB, DBCollection, or Mongo prototypes (as appropriate), and then provide thin backward-compatible shims inside rs and sh which call the real functions inside the current db (or db.getMongo()) object? Then if you have a DB or Mongo object you could just call stopBalancer() on it directly, rather than doing the dance of setting a temporary db as in the original description, and also without needing to pass the DB or Mongo object into sh or stopBalancer() somehow. (eg. a design like sh(db).stopBalancer() would break tab-completion.)

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