簡體   English   中英

nodejs和MongoDB的collection.find()不響應

[英]nodejs and MongoDB's collection.find() does not respond

我在MongoDB集合中大約有30,000個文檔。 並且一直停留在開發node.js腳本以僅檢索具有特定字符串鍵值對的記錄。

MongoDB服務器上的此查詢向我返回了我一直在尋找的確切結果:

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

立即向我返回約33個文檔。 同樣,我大約有40位名字不同的作者。

這是我的node.js腳本,用於按authorName檢索帖子(是的,它基於Name(一個字符串),因為這些作者沒有ID :():

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);
       }  
   });
}

我在這里想念什么嗎? 還有什么可以做的呢? 我使用異步方式有任何問題嗎?

PS:這里的想法是查詢轉儲,並在jPost集合中生成PDF以供認證。

PS 2:這是一個示例文檔

{
    "_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和post_comments部分。)

嘗試這個:

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);
     }
});

您是否檢查了getPosts中pAuthor的值? 因為進行聚合時會收到帶有_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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM