回答編集履歴

2

edit

2017/12/13 15:03

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -38,15 +38,15 @@
38
38
 
39
39
 
40
40
 
41
- MongoClient.connect("mongodb://localhost/"+settings.db, function(err, db) {
41
+ MongoClient.connect("mongodb://localhost/"+settings.db, function(err, client) {
42
42
 
43
43
  if (err) { return console.dir(err); }
44
44
 
45
- console.log("connected to db");
45
+ console.log("connected to server");
46
46
 
47
- const mydb = db.db('nodedb');
47
+ const db = client.db('nodedb');
48
48
 
49
- mydb.collection("users", function(err, collection) {
49
+ db.collection("users", function(err, collection) {
50
50
 
51
51
  assert.equal(err, null);
52
52
 
@@ -68,7 +68,7 @@
68
68
 
69
69
  });
70
70
 
71
- db.close();
71
+ client.close();
72
72
 
73
73
  });
74
74
 

1

edit

2017/12/13 15:03

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -17,3 +17,59 @@
17
17
 
18
18
 
19
19
  コードがあればもう少し理解が進むかもしれません。
20
+
21
+
22
+
23
+ ---
24
+
25
+
26
+
27
+ 追記:
28
+
29
+
30
+
31
+ ```node
32
+
33
+ const assert = require('assert');
34
+
35
+ const MongoClient = require('mongodb').MongoClient,
36
+
37
+ settings = require('./settings');
38
+
39
+
40
+
41
+ MongoClient.connect("mongodb://localhost/"+settings.db, function(err, db) {
42
+
43
+ if (err) { return console.dir(err); }
44
+
45
+ console.log("connected to db");
46
+
47
+ const mydb = db.db('nodedb');
48
+
49
+ mydb.collection("users", function(err, collection) {
50
+
51
+ assert.equal(err, null);
52
+
53
+ var docs = [
54
+
55
+ {name: "taguchi", score: 40},
56
+
57
+ {name: "fkoji", score: 80},
58
+
59
+ {name: "dotinstall", score: 60}
60
+
61
+ ];
62
+
63
+ collection.insert(docs, function(err, result) {
64
+
65
+ console.dir(result);
66
+
67
+ });
68
+
69
+ });
70
+
71
+ db.close();
72
+
73
+ });
74
+
75
+ ```