[SERVER-13592] How can I elegantly switch db context in a .js file? Created: 15/Apr/14  Updated: 15/Apr/14  Resolved: 15/Apr/14

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

Type: Question Priority: Major - P3
Reporter: William Cross Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I'm working in room 5025 if you want to come chat.

Context: Previously, in version 2.4, our javascript parser would allow me to pass javascript files with shell commands such as "use agg" to switch context to the agg database.

Problem: In the 2.6.0 shell, I can't pass the shell .js files with "use agg" because it isn't valid javascript.

Instead, I'm reassigning db with something like

db = new Mongo().getDB("agg")

but this causes its own problems. For instance, if I have test.js:

db = new Mongo().getDB("agg")
db.foo.insert( { a : 1 } )

then if I type mongo test.js, I get:

$ mongo test.js
MongoDB shell version: 2.6.0
connecting to: test

This is confusing for students taking the class.

I can type

 
mongo agg test.js
MongoDB shell version: 2.6.0
connecting to: agg

but the agg is doing nothing.

Is there any elegant way to specify, in a .js file, which database we're dealing with in a way that's consistent with the behavior of the script?



 Comments   
Comment by Asya Kamsky [ 15/Apr/14 ]

William, take a look at the https://github.com/mongodb/mongo/tree/master/jstests directory for some examples.

But in a nutshell you can assign to db result of db.getSiblingDB("dbnameyouwant") you can also call JS script with dbname if you are going to run against a single DB.

Comment by William Cross [ 15/Apr/14 ]

By the way, the business reason for this request is so that I can have less student confusion; we're filming revisions to our online offerings today and tomorrow, and it would be great if I had something straightforward for the students.

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