Skip to content

Instantly share code, notes, and snippets.

@simonho288
Created July 4, 2020 04:25
Show Gist options
  • Select an option

  • Save simonho288/33a4fef7ec75ccfdb85290321c580046 to your computer and use it in GitHub Desktop.

Select an option

Save simonho288/33a4fef7ec75ccfdb85290321c580046 to your computer and use it in GitHub Desktop.
const MongoClient = require('mongodb').MongoClient;
const mongoUrl = 'mongodb://localhost:27017/demo';
async function main() {
let dbc = await MongoClient.connect(mongoUrl, { useNewUrlParser: true });
let db = dbc.db('demo'); // Create a demo database
let colUsers = await db.createCollection('users');
// Delete all records at the beginning
colUsers.deleteMany({});
// Insert 4 demo user records
colUsers.insertMany([
{
_id: 1,
name: 'Mary',
registered_at: new Date(),
is_active: true
},
{
_id: 2,
name: 'John',
registered_at: new Date(),
is_active: true
},
{
_id: 3,
name: 'Peter',
registered_at: new Date(),
is_active: true
},
{
_id: 4,
name: 'Betty',
registered_at: new Date(),
is_active: true
},
]);
dbc.close();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment