Skip to content

olaekdahl/mongodb-helpers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ChatGPT MongoDB Collection Generator

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.


βœ… Features

  • 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

πŸ“¦ Requirements

  • MongoDB shell (mongosh) v1.10 or higher (must support fetch)
  • OpenAI API key
  • Internet access (to reach OpenAI)

πŸ”§ Installation

  1. Clone this repository:

    git clone https://github.com/olaekdahl/mongodb-helpers.git
  2. In chatgpt-helper.js, replace <your-openai-api-key>).


πŸ§ͺ Usage

Start mongosh and run:

load('/path/to/chatgpt-helper.js')
db.chatgpt("techstartups", 10, true)

Generate documents

You’ll get a new collection techstartups populated with AI-generated documents that include arrays.

List documents


βš™οΈ Auto-load on mongosh Start

To use this helper every time you start mongosh:

  1. Open or create your mongosh RC file:

    nano ~/.mongoshrc.js
  2. Add the following line:

    load('/path/to/chatgpt-helper.js')
  3. Save and restart mongosh. You can now use:

    db.chatgpt("techstartups", 10, true)

πŸ“˜ Function Signature

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): If true, includes nested array fields in documents (default: false).

πŸ” Security Tip

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published