2016-07-14 84 views
0

我試圖用collection.findcollection.find({})不返回的NodeJS收集記錄

var DTX = mongoose.models.dtx; 
var detx = DTX.find({}); 

來從一個集合中的所有記錄,但是,當我console.log(detx),它給了我如下的迴應:

{ _mongooseOptions: {}, 
    mongooseCollection: 
    { collection: 
     { db: [Object], 
     collectionName: 'dtx', 
     internalHint: null, 
     opts: {}, 
     slaveOk: false, 
     serializeFunctions: false, 
     raw: false, 
     pkFactory: [Object], 
     serverCapabilities: undefined }, 
    opts: { bufferCommands: true, capped: false }, 
    name: 'ddtx', 
    conn: 
     { base: [Object], 
     collections: [Object], 
     models: [Object], 
     replica: false, 
     hosts: null, 
     host: 'localhost', 
     port: 27017, 
     user: undefined, 
     pass: undefined, 
     name: 't23', 
     options: [Object], 
     otherDbs: [], 
     _readyState: 1, 
     _closeCalled: false, 
     _hasOpened: true, 
     _listening: true, 
     _events: [Object], 
     db: [Object] }, 
    queue: [], 
    buffer: false }, 
    model: 
    { [Function: model] 
    base: 
     { connections: [Object], 
     plugins: [], 
     models: [Object], 
     modelSchemas: [Object], 
     options: [Object], 
     dbConnected: true }, 
    modelName: 'dtx', 
    model: [Function: model], 
    ..... 
    } 
    ..... 
    } 
    ..... 
} 

我讀了幾個帖子,然後我意識到在nodejs這返回cursor。那麼我嘗試如下:

var detx=DTX.find({},function(err,cursor){ 
      cursor.each(err,item){ 
       return item; 
      }); 
     }); 

這也沒有幫助。如何在沒有任何條件的情況下從集合中獲取所有記錄作爲array

+0

@RolandStarke ..嗯,我希望這AINT這種情況下。隨着'query' PARAMS傳遞給'find',它會給我的數據,但不是空.. –

回答

1

試試這個。

var DTX = mongoose.models.dtx; 
var detx = []; 
DTX.find({}) 
    .exec(function (err, dtxArray) { 

     if (err) 
     console.log(err); 

     console.log('DTX Array: ', dtxArray); 

     detx = dtxArray; 
}); 
+0

這就是它哥們..謝謝了。 。:) –