Created
July 4, 2020 04:25
-
-
Save simonho288/33a4fef7ec75ccfdb85290321c580046 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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