Auto insert mgeneratejs documents into a collection

Hi everybody,
I would like to know what’s the commands for inserting mgeneratejs’s documents into a specified collection?

Thank you in advance for your cooperation.

PS: when I use “mgenerate” it says:

mgenerate is no longer included with mtools. Please use instead.

Check this link

Mgenerate is no longer included with mtools

Yes, and that’s why I used “mgeneratejs”

Hi @Hamid_Saadi,

You can pipe the output of mgeneratejs to mongoimport.

For example:

mgeneratejs ‘{“name”: “$name”}’ -n 1000 | mongoimport -d mydb -c mycollection

I recommend saving your JSON template in a file so you don’t have to worry about properly quoting/escapting strings in your shell environment:

mgeneratejs -n 1000 mytemplate.json | mongoimport -d mydb -c mycollection

If you want to generate test data based on sampling existing data, here’s a fun command line recipe combining mgeneratejs with some other command line tools:

The above examples are connecting to a local dev server, but normally you would be including a --uri parameter for mongoimport with your connection string. For example, using MongoDB Atlas: Load File with mongoimport.

mgenerate is an older Python implementation that was succeeded by the more flexible mgeneratejs from the same author. mgenerate was deprecated and eventually removed from mtools in 2017.


1 Like

Thanks a lot, It was helpful.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.