Skip to content

Instantly share code, notes, and snippets.

@keidarcy
Created December 19, 2022 08:50
Show Gist options
  • Save keidarcy/688f3fe6d5ca8351078fc086bc37ec85 to your computer and use it in GitHub Desktop.
Save keidarcy/688f3fe6d5ca8351078fc086bc37ec85 to your computer and use it in GitHub Desktop.
Minimum setup to create new mongodb database programmatically

command

node index.js test1

node index.js test2

result

Screen Shot 2022-12-19 at 17 46 44

const mongoose = require('mongoose');
const kittySchema = new mongoose.Schema({
name: String
});
const Kitten = mongoose.model('Kitten', kittySchema);
const CONNECTION_STRING = process.env.MONGODB_TEST_URL + '/' + process.argv[2]
async function main() {
await mongoose.connect(CONNECTION_STRING);
const cat = new Kitten({ name: 'cat one' });
await cat.save();
const cats = await Kitten.find();
console.log({ cats })
process.exit(0)
}
main().catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment