2016-04-27 8 views
0

MongoDBコレクションには約30,000のドキュメントがあります。また、特定の文字列のキーと値のペアを持つレコードのみを取得するためにnode.jsスクリプトを開発することにも取り組みませんでした。nodejsとMongoDBのcollection.find()は応答しません

のMongoDBサーバー上でこのクエリは、私が探していた正確な結果を私に返します。

db.getCollection('posts').find({authorName: "Ashwin-kumar"}) 

は即座に私に約33の文書を返します。同様に、名前が異なる40人ほどの著者がいます。

ここではauthorName(これらの著者のためのIDがないとはい、それは、名前、文字列に基づいています:()での投稿を取得するために私のNode.jsスクリプトです:これはいないようです

var fs = require('fs'), 
    request = require('request'), 
    async = require("async"), 
    assert = require('assert'); 
    _ = require('lodash'), 
    MongoClient = require('mongodb').MongoClient; 

var db, postsCollection, postCol; 

async.series([dbConnect, checkCollection, createMeta, dbClose], function(){ 
    console.log("Executed all calls in series."); 
    process.exit(0); 
}); 

function dbConnect(callback){ 
    MongoClient.connect("mongodb://localhost:27017/jPosts", function(pErr, pDb) { 
     if(pErr) { 
      console.dir(pDb); 
      return 0; 
     } 
     db = pDb; 
     callback(); 
    }); 
} 

function dbClose(callback){ 
    db.close(true, function (err) { 
     if (err) console.error(err); 
     else console.log("close complete"); 
     callback(); 
    }); 
} 

function checkCollection(callback) { 
    db.collection('posts', function(err, collection) {}); 
    postsCollection = db.collection('posts'); 
    postCol = db.collection('posts'); 
    callback(); 
} 

function createMeta(callback){ 
    var meta = []; 
    postsCollection.aggregate([ 
     { 
      $group : {_id : "$authorName"} 
     }]).toArray(function(err, result) { 
      assert.equal(err, null); 
      async.forEachLimit(result, 1, function(pPost, callback) { 
       getPosts(pPost._id, callback); 
      }, function(err) { 
       console.log(err); 
       callback(); 
      });     
     }); 
} 

function getPosts(pAuthor, callback){ 
    var cursor = postCol.find({ "authorName": pAuthor}); 
    cursor.toArray(function(err,items){ 
     if(err) 
      callback(err); 
      else 
      callback(null, items); 
     }); 
} 

は、 )(cursor.toArray。私のために働く何もしませんが、永遠に待機する。なぜなら、各文書であまりにも多くのフィールドで、それはありますか?

私は、カーソルフェッチされたドキュメントの数を取得しようとしましたし、それがうまく動作します。

function getPosts(pAuthor, callback){ 
    var cursor = postCol.find({ "authourName": pAuthor}); 
    cursor.count().then(function(items_count) { 
     console.log(items_count); 
     callback(); 
    });   
} 

また、カーソルの.eachメソッドを使用して、取得したドキュメントを反復処理しました。しかし、運はまだありません。

function getPosts(pAuthor, callback){ 
    var cursor = postCol.find({ "authourName": pAuthor}); 
    cursor.each(function(err, doc) { 
     assert.equal(err, null); 
     if (doc != null) { 
      console.dir(doc); 
     } else { 
      console.log(err); 
     } 
    }); 
} 

ここに何か不足していますか?この仕事をするために他に何ができますか?私が非同期を使用している方法に問題はありますか?

P.S:ここでのアイデアは、ダンプを照会し、jPostコレクションのauthours用のPDFを生成することです。

P.S 2:ここでは、としてgetPostsでpAuthorの値が何であるかのサンプル文書

{ 
    "_id" : ObjectId("571d36b55672f713fe346a66"), 
    "id" : 56517, 
    "authorName" : "Ashwin-kumar", 
    "comment_count" : 380, 
    "tagline" : "... Opinions you don't really need", 
    "vote_count" : 5152, 
    "exclusive" : null, 
    "post": [ 
    ], 
    "post_comments" : [ 
     //comment_count objects 
    ], 
    "date" : "2016-03-27" 
} 

(。私は簡潔にするため、ポスト& post_comments部品を省略しました)

答えて

0

確認しましたですか?あなたが集約を行うとき、あなたは_idフィールド(ないauthourName)とオブジェクトのコレクションを受けているので、あなたが行う必要があります。

// not sure why you need meta array, at least it's not used in the code you provided 
meta.push({ 
    author: pPost._id 
}); 
getPosts(pPost._id, callback); 
+0

メタ配列が異なるため、他の場所で使用されます目的。そして、はい、著者の集計返却リストと投稿数 –

+0

右、pPost._idは私の実際のコードで使用しているものです。ここに投稿する関数全体をクリーンアップすると、私はpPost._idをauthorNameに置き換えました。 –

+0

ああ、どのasyncのバージョンを使用していますか? https://github.com/caolan/asyncのドキュメントにforEachLimitがないので、同じ目的でasync.eachLimitを使用します。しかし、ライブラリのバージョンは問題の原因とは考えにくいでしょう。 –

1

はこれを試してください:

var collection = db.collection("collection_name"); 
collection.find({authourName: "Ashwin-kumar"}).toArray(function (err,items) { 
    if (err) { 
     console.dir(err); 
    } else { 
     //do something with items array 
     console.dir(items); 
    } 
}); 
+0

"collection_name"はあなたのコレクションの名前です – SU15

関連する問題