简体   繁体   English

nodejs和MongoDB的collection.find()不响应

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

I have about 30,000 documents in a MongoDB collection. 我在MongoDB集合中大约有30,000个文档。 And have been stuck in developing a node.js script to retrieve only the records with a specific string key-value pair. 并且一直停留在开发node.js脚本以仅检索具有特定字符串键值对的记录。

this query on MongoDB server returns me the exact results I've been looking for: MongoDB服务器上的此查询向我返回了我一直在寻找的确切结果:

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

Returns me about 33 documents instantly. 立即向我返回约33个文档。 Likewise I've about 40 authors with different names. 同样,我大约有40位名字不同的作者。

Here's my node.js script to retrieve posts by authorName (Yes, it is based on Name, a string, as there is no ID for these authors :( ): 这是我的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);
        });
}

This does not seem to work for me. 这似乎对我不起作用。 cursor.toArray() does nothing but wait forever. cursor.toArray()只会永远等待。 Is it because of too many fields in each document? 是否因为每个文档中的字段太多?

I tried to get the count of the documents the cursor fetched and it works well. 我试图获取光标获取的文档数,并且效果很好。

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

Also, I tried the cursor's .each method to iterate the documents fetched. 另外,我尝试使用游标的.each方法来迭代获取的文档。 But no luck yet. 但是还没有运气。

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

Am I missing something here? 我在这里想念什么吗? What else can be done to make this work? 还有什么可以做的呢? Is there any issues with the way I'm using async? 我使用异步方式有任何问题吗?

PS: The idea here is to query the dump and generate the PDF's for authours in the jPost collection. PS:这里的想法是查询转储,并在jPost集合中生成PDF以供认证。

PS 2: Here's a sample document 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"
}

(I've omitted post & post_comments parts for brevity.) (为简便起见,我省略了post和post_comments部分。)

try this: 尝试这个:

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

Did you check what is the value of pAuthor in getPosts? 您是否检查了getPosts中pAuthor的值? Because when you do aggregation, you receive a collection of objects with _id field (not authourName), so you should do: 因为进行聚合时会收到带有_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.

相关问题 在nodejs collection.find()中没有从Mongodb集合中获取值,它什么也不返回 - In nodejs collection.find() is not fetching values from Mongodb collection.It returning nothing MongoDB collection.find()查询挂起 - MongoDB collection.find() query hanging 在快速app.js文件中使用mongodb的collection.find()时出现问题 - Issue with mongodb's collection.find() when used in an express app.js file 车把模板访问Node.js中的mongo collection.find()变量 - Handlebar template access to mongo collection.find() variable in a nodejs mongodb:使用$ in运算符的collection.find不返回任何内容 - mongodb: collection.find with $in operator doesn't return anything 代码停滞在collection.find - Code stalling at collection.find 如何访问mongodb collection.find方法返回的Expressjs中的特定字段? - How to access a specific field in Expressjs returned by mongodb collection.find method? node.js mongodb-collection.find()。toArray(callback)返回空 - node.js mongodb - collection.find().toArray(callback) returns empty node.js mongodb - collection.find()。toArray(callback) - 不调用callback - node.js mongodb - collection.find().toArray(callback) - callback doesn't get called Express 和 MongoDB,使用 $text 和 $search 时 collection.find 中出现转义字符的问题 - Express and MongoDB, problem with escape characters in the collection.find while using $text and $search
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM