This script extends mongosh
with a helper function:
await db.chatgpt(keyword, count = 5, arrays = false)
It uses the OpenAI API to generate realistic JSON documents based on a keyword and inserts them into a MongoDB collection.
- Generate synthetic data using ChatGPT (via OpenAI API)
- Creates a new MongoDB collection (based on keyword)
- Supports nested array fields for richer document structure
- Integrates natively into
mongosh
- MongoDB shell (
mongosh
) v1.10 or higher (must supportfetch
) - OpenAI API key
- Internet access (to reach OpenAI)
-
Clone this repository:
git clone https://github.com/olaekdahl/mongodb-helpers.git
-
In
chatgpt-helper.js
, replace<your-openai-api-key>
).
Start mongosh
and run:
load('/path/to/chatgpt-helper.js')
db.chatgpt("techstartups", 10, true)
Youβll get a new collection techstartups
populated with AI-generated documents that include arrays.
To use this helper every time you start mongosh
:
-
Open or create your mongosh RC file:
nano ~/.mongoshrc.js
-
Add the following line:
load('/path/to/chatgpt-helper.js')
-
Save and restart
mongosh
. You can now use:db.chatgpt("techstartups", 10, true)
await db.chatgpt(keyword, count = 5, arrays = false)
keyword
(string): Topic or type of documents to generate.count
(number, optional): Number of documents to generate (default: 5).arrays
(boolean, optional): Iftrue
, includes nested array fields in documents (default:false
).
Avoid committing your OpenAI API key to version control. You can instead read it from a config file and inject into .mongoshrc.js
dynamically if needed.