Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load initial data in MongoDB?

Tags:

mongodb

nosql

Does anyone know how to populate mongodb with initial data? For example, with a traditional SQL database, you can put all your SQL statements in a textfile, then load that using a SQL command. This is extremely useful for unit testing.

Is it possible to do this with the mongo shell? For example, write down a list of shell statements into a file, and get the mongo shell to read the file and execute the statements.

like image 969
Erin Drummond Avatar asked Aug 08 '10 12:08

Erin Drummond


People also ask

How do I find my first file in MongoDB?

find(); > var document = collection[0]; In the Mongo shell, find() returns a cursor, not an array. In the docs you can see the methods you can call on a cursor. findOne() returns a single document and should work for what you're trying to accomplish.


1 Answers

./mongo server:27017/dbname --quiet my_commands.js

In my_commands.js:

db.users.insert({name:"john", email:"[email protected]", age:12}); 
like image 167
Mark Avatar answered Oct 23 '22 20:10

Mark