-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_test.js
33 lines (26 loc) · 900 Bytes
/
mongo_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
// connect to mongo
MongoClient.connect(url, {useUnifiedTopology: true}, function(err, client) {
console.log("Connected successfully to server");
// database Name
const dbName = 'myproject';
const db = client.db(dbName);
// new user
var name = 'user' + Math.floor(Math.random()*10000);
var email = name + '@mit.edu';
// insert into customer table
var collection = db.collection('customers');
var doc = {name, email};
collection.insertOne(doc, {w:1}, function(err, result) {
console.log('Document insert');
});
var customers = db
.collection('customers')
.find()
.toArray(function(err, docs) {
console.log('Collection:',docs);
// clean up
client.close();
});
});