2

I have a couple of Ids in my mongodb database document. I need to create indexes on these Ids through a script so that i do not have to run the ensureIndex command again and again.

db.getCollection("elements").ensureIndex({customerId:1});
db.getCollection("elements").ensureIndex({userId:1});
........

I just run the script (that contains all these commands) and I should be done. Any idea how to run a script from mongodb shell? Also what should be the extension of the script?

Juhi.

2
  • Do you want to run it again, or what? Commented Jan 5, 2012 at 13:33
  • 2
    You should accept helpful answers, by the way. Commented Jan 6, 2012 at 22:43

3 Answers 3

14

You can put your commands to a javascript file (with .js extension) and use mongo console utility to run it. Like this:

mongo --host my_host --port my_port my_db create_indexes.js

Where create_indexes.js file will contain your commands:

db.getCollection("elements").ensureIndex({customerId:1});
db.getCollection("elements").ensureIndex({userId:1});
....
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, also take a look here for some pointers: mongodb.org/display/DOCS/Scripting+the+shell
Works good for me.. :) BTW.. Will it work fine in a Sharded enviornment? I mean if i run it on the mongos shell will it reflect on the shards I am running or i ll have to run the .js separately on my shards?
If you run it against mongos, it will execute on all shards.
9

You can also run the script from the mongodb shell. I like adding things to a function to make it convenient to call them. Create the script like this:

var ensureIndexes = function() {
    db.getCollection("elements").ensureIndex({customerId:1});
    db.getCollection("elements").ensureIndex({userId:1}); ........
}

Save the above to a file named ensureIndexes.js, then from the mongo shell enter:

load('ensureIndexes.js')

Now you will be able to enter ensureIndexes() to call that function. I have a lot of scripts containing utility functions like that.

2 Comments

This is a nice trick to work with, that will ensure our custom functions other than the js functions provided by mongodb shell.. :) BTW... Will it work fine in a Sharded enviornment? I mean if i run it on the mongos shell will it reflect on the shards I am running or i ll have to run the .js separately on my shards?
@juhi Yes, you can do that. See this page: mongodb.org/display/DOCS/Server-side+Code+Execution especially the section on storing scripts server-side. You can then run them anywhere.
4

If you are using any unix like OS, it might help.

I did a redirect using unix shell.

So from UNIX command line you type:

mongo < your_script.js

Inside the your_script.js you can state your commands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.