Reputation: 61
Could you please help me out in writing Mongo Shell script to create new collections and indexes to it. Here are some part of the code which i wrote and i need to refactored it.
db.createCollection("Dog", { autoIndexId : true});
db.createCollection("Cat", { autoIndexId : true});
db.createCollection("Mouse", { autoIndexId : true});
db.createCollection("Name", { autoIndexId : true});
In above code can i write single statement to create multiple collections? and also add multiple indexes to one collection in one go?
Upvotes: 2
Views: 8435
Reputation: 14317
Create a file called as "my_scripts.js", with the following commands. This JavaScript defines the data and commands to create two collections (dogs
and cats
), their data (documents as JSON) and the indexes (indexes are created on the name
field of the collection).
my_scripts.js:
let dogDocs = [
{
name: "pooch",
breed: "poodle",
weight: "6 lbs"
},
{
name: "mutt",
breed: "bulldog",
weight: "10 lbs"
}
];
let catDocs = [
{
name: "minni",
breed: "persian",
color: "white"
},
{
name: "tinkle",
breed: "bombay",
color: "black"
}
];
let dogIndex = { name : 1 };
let catIndex = { name : 1 };
let collInfoObjs = [
{ coll: "dogs", data: dogDocs, index: dogIndex },
{ coll: "cats", data: catDocs, index: catIndex }
];
for (obj of collInfoObjs) {
db[obj.coll].insertMany(obj.data);
db[obj.coll].createIndex(obj.index);
}
Run the script file:
From the mongo shell run the script as (you can specify the file path with the load
command):
mongo > load("my_script.js");
After the script is run, you can use the following commands individually to verify the collections, their documents and the indexes:
db.dogs.find();
db.cats.find();
db.dogs.getIndexes();
db.cats.getIndexes();
Note that the documents will have a unique _id
field (of type ObjectId
) created if you don't supply the _id
in the input JSON data.
Upvotes: 5
Reputation: 1348
We have a whole page on writing scripts for the shell. Scripts that are executed from a file use a slightly different syntax for creating connections and getting databases e.g.:
Opening connections:
new Mongo()
new Mongo(<host>)
new Mongo(<host:port>)
Getting a database:
conn = new Mongo();
db = conn.getDB("myDatabase");
Upvotes: -1
Reputation: 516
Mongo Shell scripts are just JavaScript scripts. You can use regular JavaScript control structures like loops, and string interpolation / string manipulation operations for generating collection names.
I'm guessing it will be
["Dog", "Cat", "Mouse", "Name"].forEach(function(collName) {
db.createCollection(collName, {autoIndexId: true});
});
Upvotes: 0
Reputation: 14317
From the Mongo shell:
> use myNewDB;
> var myCollections = [ "Dog", "Cat", "Mouse", "Name" ];
> myCollections.forEach( collName => db.createCollection( collName, { autoIndexId : true} ) );
// -or-
> [ "Dog", "Cat", "Mouse", "Name" ].forEach( collName => db.createCollection( collName ) );
> show collections
Cat
Dog
Mouse
Name
NOTE:
{ autoIndexId : true}
option is deprecated since MongoDB version 3.2; it is about creating index on _id
field. The _id
's unique index is mandatory now and is created automatically. So you don't have to specify the option (and cannot specify false
option; it will give an error).
You can create a detailed script to add indexes after a collection is created, further. See Write scripts from Mongo Shell.
Upvotes: 0