前提・実現したいこと
Node.jsでmongoDBの検索をしています。
ある特定キーに対するバリューのみを出したいのですが、うまくいきません。
調べてみても、例えばdist:1のものを出力するようにするものはありますが、distの値そのものを出力させるものは見当たりません。
全体のドキュメントは表示できる状態ですので検索の仕方がわからないという問題だとおもいます。
MongoDBには以下のようにid、dist(配列)、rot(配列)が入っています。
これらのdist、rotの配列のみを変数に格納したいのですが、どのようにすればいいでしょうか。
分かる方がいましたら、回答お願いします。
[{ id:~~~, dist:[ 457, 442, ・・・ ], rot:[0.01, 0.14, ・・・ ] }]
該当のソースコード
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'study'; //結果 var result=new Array(); const client = new MongoClient(url,{useNewUrlParser: true}); // Use connect method to connect to the server client.connect(function(err) { assert.equal(null, err); console.log("Connected correctly to server"); const db = client.db(dbName); findDocuments(db, function() { client.close(); }); }); const findDocuments = function(db, callback) { // Get the documents collection const collection = db.collection('user'); // Find some documents collection.find({}).toArray(function(err, docs) { assert.equal(err, null); console.log("Found the following records"); for (var document of docs) { console.log("Found the following records"); //console.log(document[dist]); //console.log(document[rot]); //result=push(document.dist); //result=push(document.rot); } //console.log(docs) callback(docs); }); }
試したこと
document.dist
document[dist]
これらはエラーになりました。
補足情報(FW/ツールのバージョンなど)
mongodb:4.0.10
OS:Ubuntu
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/01 01:59