質問編集履歴
2
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
Node.jsでmongoDBの検索をしています。
|
6
6
|
|
7
7
|
ある特定キーに対するバリューのみを出したいのですが、うまくいきません。
|
8
|
+
|
9
|
+
調べてみても、例えばdist:1のものを出力するようにするものはありますが、distの値そのものを出力させるものは見当たりません。
|
8
10
|
|
9
11
|
全体のドキュメントは表示できる状態ですので検索の仕方がわからないという問題だとおもいます。
|
10
12
|
|
1
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
Node.jsでmongoDBの検索をしています。
|
6
6
|
|
7
7
|
ある特定キーに対するバリューのみを出したいのですが、うまくいきません。
|
8
|
+
|
9
|
+
全体のドキュメントは表示できる状態ですので検索の仕方がわからないという問題だとおもいます。
|
8
10
|
|
9
11
|
MongoDBには以下のようにid、dist(配列)、rot(配列)が入っています。
|
10
12
|
|
@@ -48,7 +50,91 @@
|
|
48
50
|
|
49
51
|
```ここに言語名を入力
|
50
52
|
|
53
|
+
const MongoClient = require('mongodb').MongoClient;
|
54
|
+
|
55
|
+
const assert = require('assert');
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
// Connection URL
|
60
|
+
|
61
|
+
const url = 'mongodb://localhost:27017';
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
// Database Name
|
66
|
+
|
67
|
+
const dbName = 'study';
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
//結果
|
72
|
+
|
73
|
+
var result=new Array();
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
const client = new MongoClient(url,{useNewUrlParser: true});
|
78
|
+
|
79
|
+
// Use connect method to connect to the server
|
80
|
+
|
81
|
+
client.connect(function(err) {
|
82
|
+
|
83
|
+
assert.equal(null, err);
|
84
|
+
|
85
|
+
console.log("Connected correctly to server");
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
const db = client.db(dbName);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
findDocuments(db, function() {
|
94
|
+
|
95
|
+
client.close();
|
96
|
+
|
51
|
-
|
97
|
+
});
|
98
|
+
|
99
|
+
});
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
const findDocuments = function(db, callback) {
|
104
|
+
|
105
|
+
// Get the documents collection
|
106
|
+
|
107
|
+
const collection = db.collection('user');
|
108
|
+
|
109
|
+
// Find some documents
|
110
|
+
|
111
|
+
collection.find({}).toArray(function(err, docs) {
|
112
|
+
|
113
|
+
assert.equal(err, null);
|
114
|
+
|
115
|
+
console.log("Found the following records");
|
116
|
+
|
117
|
+
for (var document of docs) {
|
118
|
+
|
119
|
+
console.log("Found the following records");
|
120
|
+
|
121
|
+
//console.log(document[dist]);
|
122
|
+
|
123
|
+
//console.log(document[rot]);
|
124
|
+
|
125
|
+
//result=push(document.dist);
|
126
|
+
|
127
|
+
//result=push(document.rot);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
//console.log(docs)
|
132
|
+
|
133
|
+
callback(docs);
|
134
|
+
|
135
|
+
});
|
136
|
+
|
137
|
+
}
|
52
138
|
|
53
139
|
```
|
54
140
|
|
@@ -58,7 +144,11 @@
|
|
58
144
|
|
59
145
|
|
60
146
|
|
147
|
+
document.dist
|
148
|
+
|
149
|
+
document[dist]
|
150
|
+
|
61
|
-
こ
|
151
|
+
これらはエラーになりました。
|
62
152
|
|
63
153
|
|
64
154
|
|