MongoClient版本的find()实际上返回了什么?

时间:2015-11-11 21:02:42

标签: javascript node.js mongodb mongodb-query database

我正在使用原生node.js mongodb驱动程序,只是对一个集合进行了基本的find()操作,它返回了,我不明白。我不希望知道关于这个对象的一切,只是对于初学者我想知道我应该在哪里开始为我想要的集合解析这个对象。

其他有趣的事实:我使用的是OsX。

感谢您的关注!我将在下面包含此对象...

Readable {
  connection: null,
  server: null,
  disconnectHandler: 
   { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
     length: [Getter] },
  bson: {},
  ns: 'epc.customers',
  cmd: 
   { find: 'epc.customers',
     limit: 0,
     skip: 0,
     query: {},
     slaveOk: true,
     readPreference: { preference: 'primary', tags: undefined, options: undefined } },
  options: 
   { skip: 0,
     limit: 0,
     raw: undefined,
     hint: null,
     timeout: undefined,
     slaveOk: true,
     readPreference: { preference: 'primary', tags: undefined, options: undefined },
     db: 
      EventEmitter {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        s: [Object],
        serverConfig: [Getter],
        bufferMaxEntries: [Getter],
        databaseName: [Getter],
        options: [Getter],
        native_parser: [Getter],
        slaveOk: [Getter],
        writeConcern: [Getter] },
     promiseLibrary: [Function: Promise],
     disconnectHandler: { s: [Object], length: [Getter] } },
  topology: 
   EventEmitter {
     domain: null,
     _events: 
      { reconnect: [Function],
        timeout: [Object],
        error: [Object],
        close: [Function],
        destroy: [Object] },
     _eventsCount: 5,
     _maxListeners: undefined,
     s: 
      { options: [Object],
        callbacks: [Object],
        logger: [Object],
        state: 'connected',
        reconnect: true,
        reconnectTries: 30,
        reconnectInterval: 1000,
        emitError: true,
        currentReconnectRetry: 30,
        ismaster: [Object],
        readPreferenceStrategies: undefined,
        authProviders: [Object],
        id: 1,
        tag: undefined,
        disconnectHandler: [Object],
        wireProtocolHandler: {},
        Cursor: [Object],
        bsonInstance: {},
        bson: {},
        pool: [Object],
        isMasterLatencyMS: 2,
        serverDetails: [Object] },
     name: [Getter],
     bson: [Getter],
     wireProtocolHandler: [Getter],
     id: [Getter] },
  cursorState: 
   { cursorId: null,
     cmd: 
      { find: 'epc.customers',
        limit: 0,
        skip: 0,
        query: {},
        slaveOk: true,
        readPreference: [Object] },
     documents: [],
     cursorIndex: 0,
     dead: false,
     killed: false,
     init: false,
     notified: false,
     limit: 0,
     skip: 0,
     batchSize: 1000,
     currentLimit: 0,
     transforms: undefined },
  callbacks: null,
  logger: { className: 'Cursor' },
  _readableState: 
   ReadableState {
     objectMode: true,
     highWaterMark: 16,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: null,
     ended: false,
     endEmitted: false,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  s: 
   { numberOfRetries: 5,
     tailableRetryInterval: 500,
     currentNumberOfRetries: 5,
     state: 0,
     streamOptions: {},
     bson: {},
     ns: 'epc.customers',
     cmd: 
      { find: 'epc.customers',
        limit: 0,
        skip: 0,
        query: {},
        slaveOk: true,
        readPreference: [Object] },
     options: 
      { skip: 0,
        limit: 0,
        raw: undefined,
        hint: null,
        timeout: undefined,
        slaveOk: true,
        readPreference: [Object],
        db: [Object],
        promiseLibrary: [Function: Promise],
        disconnectHandler: [Object] },
     topology: 
      EventEmitter {
        domain: null,
        _events: [Object],
        _eventsCount: 5,
        _maxListeners: undefined,
        s: [Object],
        name: [Getter],
        bson: [Getter],
        wireProtocolHandler: [Getter],
        id: [Getter] },
     topologyOptions: 
      { socketOptions: {},
        auto_reconnect: true,
        host: 'localhost',
        port: 27017,
        cursorFactory: [Object],
        reconnect: true,
        emitError: true,
        size: 5,
        disconnectHandler: [Object],
        bson: {},
        messageHandler: [Function],
        wireProtocolHandler: {} },
     promiseLibrary: [Function: Promise],
     currentDoc: null },

晚间

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert')
  , ObjectId = require('mongodb').ObjectId;

// Connection URL
var url = 'mongodb://localhost:27017/eProveCommons';
// Use connect method to connect to the Server

var findAll = function(db, callback) {
    var collection = db.collection('all');

    collection.find().toArray(function(err, docs){
        assert.equal(err, null);
        console.log(docs);
        callback(docs);
    })
}


MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    console.log('You are connected correctly to the server.');
    findAll(db, function(docs){
        exports.getAll = function(){
            return docs;
        }
        db.close();
    });
});

1 个答案:

答案 0 :(得分:0)

这是一个游标对象。

使用光标,您可以执行类似

的操作
var cursor = collection.find({});
cursor.each(...);

有关详细信息,请参阅此链接:https://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html

注意:如果您知道结果集较小,可以使用find({})。toArray()返回文档列表。