-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples-1.js
46 lines (36 loc) · 2.19 KB
/
examples-1.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
34
35
36
37
38
39
40
41
42
43
44
45
46
Object.assign = require('object-assign')
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://localhost:27017/chat';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
// HURRAY!! We are connected. :)
console.log('Connection established to', url);
// Get the documents collection
var collection = db.collection('users');
//Create some users
var user1 = { index: 1, username: 'lauraguirre', name: 'Laura Aguirre', twitter: 'https://twitter.com/lauraguirre011', emails: ['[email protected]'], roles: ['admin', 'moderator', 'user'], createdAt: new Date() };
var user2 = { index: 2, username: 'felipesalazar', name: 'Felipe Salazar', twitter: 'https://twitter.com/elpipesalazar', emails: ['[email protected]'], roles: ['moderator'], createdAt: new Date()};
var user3 = { index: 3, username: 'jdmorales', name: 'John Morales', twitter: '', emails: ['[email protected]'], roles: ['moderator', 'user'], createdAt: new Date()};
var user4 = { index: 4, username: 'juankzuluaga', name: 'Juan Zuluaga', twitter: 'https://twitter.com/Juankzu', emails: ['[email protected]'], roles: ['user'], createdAt: new Date()};
var user5 = { index: 5, username: 'xergioalex', name: 'Sergio Alexander', twitter: 'https://twitter.com/xergioalex', emails: ['[email protected]', '[email protected]'], roles: ['admin', 'moderator', 'user'], createdAt: new Date()};
// Insert some users
collection.insert([user1, user2, user3, user4, user5], function (err, result) {
if (err) {
console.log(err);
// Close connection
db.close();
} else {
console.log('Inserted %d documents into the "users" collection. The documents inserted with "_id" are:', result.result.n, result);
// Close connection
db.close();
}
});
}
});