Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run mongodb script once to insert initial data

I have a chicken and egg problem with my node server in which you need to have a user with a certain role that has certain permissions to be able to log in and start creating more users, roles, etc.

I would like to initialize the database such that I create an initial ADMIN role and initial admin user that has that role.

I.E. started with a script and ran into problems:

use mydb

db.roles.insert({
  name: "ADMIN_ROLE",
  description: "Administrative role",
  permissions: ['ALL']
});

db.users.insert({
  username: "admin",
  password: "password",
  role: ??? (get ADMIN_ROLE _id from above)
});

Basically I ran into a couple of problems: 1. not really sure if I can script like this. 2. How to get ADMIN_ROLE id to store in new admin user

Another idea: Write a quick node app that connects to mongodb and inserts the proper stuff. Anyone done this before.

And yet another: Does anything like ruby rake exist for node/mongo. I.E. the initial seed may not be the only data I need to 'manually' mess with. I.E. I might need to patch the database at some point in time. Would be nice to create patch #1 as the initial seed, and then be able to write future patches if necessary and be able to. I.E. anything like rake migrate?

Any other ideas on how to seed a mongo database?

like image 287
lostintranslation Avatar asked Jun 28 '13 18:06

lostintranslation


People also ask

How do I run an insert statement in MongoDB?

In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a unique ObjectId for this document. You can also pass an array of documents into the insert() method as shown below:. To insert the document you can use db. post.


1 Answers

Shoot just found this:

https://github.com/visionmedia/node-migrate

and

https://npmjs.org/package/mongo-migrate

Exactly what I was looking for.

like image 111
lostintranslation Avatar answered Oct 19 '22 03:10

lostintranslation