开发者

How to load initial data in MongoDB?

开发者 https://www.devze.com 2023-01-10 12:41 出处:网络
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

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 exa开发者_运维百科mple, write down a list of shell statements into a file, and get the mongo shell to read the file and execute the statements.


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

In my_commands.js:

db.users.insert({name:"john", email:"john@doe.com", age:12});


You can use the mongoimport tool that comes with MongoDB to import raw data.

To run scripts from a file, e.g. to recreate indexes, pass the file name as a command line argument:

mongo file.js "another file.js"
0

精彩评论

暂无评论...
验证码 换一张
取 消