-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmongo-insert.js
More file actions
62 lines (58 loc) · 1.39 KB
/
mongo-insert.js
File metadata and controls
62 lines (58 loc) · 1.39 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var insertUsers = function(db, callback) {
var collection = db.collection('users');
// Insert some documents
collection.insertMany([
{
username: 'admin',
password: 'admin'
},
{
username: 'staff',
password: 'staff'
},
{
username: 'user',
password: 'user'
}
], function(err, result) {
console.log("Inserted 3 users into the document collection");
callback(result);
})
}
var insertDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('products');
// Insert some documents
collection.insertMany([
{
name: 'iphone',
year: 2010
},
{
name: 'moto',
year: 2015
},
{
name: 'nexus',
year: 2016
}
], function(err, result) {
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the document collection");
callback(result);
});
}
// Connection URL
var url = 'mongodb://localhost:27017/products';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertUsers(db, function() {
db.close();
});
});