什么是mongodb中db.collection.find()的返回类型

时间:2015-03-18 02:34:31

标签: javascript node.js mongodb

我正在尝试从mongodb连接中读取find函数的输出。

我想将数据库的结果存储到名为result的结果的JSON / Array中:

collection= db.collection('users');
result =collection.find().

现在这个代码在结果中我得到了一些奇怪的长json输出:

{ db: 
{ domain: null,
 _events: {},
 _maxListeners: 10,
 databaseName: 'askr',
 serverConfig: 
  { domain: null,
    _events: [Object],
    _maxListeners: 10,
    auth: [Getter],
    _callBackStore: [Object],
    _commandsStore: [Object],
    _dbStore: [Object],
    options: [Object],
    _serverState: 'connected',
    _haProcess: [Object],
    servers: [Object],
    strategyInstance: [Object],
    emitOpen: false,
    _state: [Object],
    _currentServerChoice: 0,
    socketTimeoutMS: [Getter/Setter],
    _used: true,
    logger: [Object] },
 options: 
  { read_preference_tags: null,
    read_preference: 'primary',
    url: 'mongodb://vishal:admin@ds055680.mongolab.com:55680/askr',
    native_parser: true,
    readPreference: [Object],
    safe: false,
    w: 1 },
 _applicationClosed: false,
 slaveOk: false,
 bufferMaxEntries: -1,
 native_parser: true,
 bsonLib: 
  { BSON: [Object],
    Long: [Object],
    ObjectID: [Object],
    DBRef: [Object],
    Code: [Object],
    Timestamp: [Object],
    Binary: [Object],
    Double: [Object],
    MaxKey: [Object],
    MinKey: [Object],
    Symbol: [Object] },
 bson: { promoteLongs: true },
 bson_deserializer: 
  { Code: [Object],
    Symbol: [Object],
    BSON: [Object],
    DBRef: [Object],
    Binary: [Object],
    ObjectID: [Object],
    Long: [Object],
    Timestamp: [Object],
    Double: [Object],
    MinKey: [Object],
    MaxKey: [Object],
    promoteLongs: true },
 bson_serializer: 
  { Code: [Object],
    Symbol: [Object],
    BSON: [Object],
    DBRef: [Object],
    Binary: [Object],
    ObjectID: [Object],
    Long: [Object],
    Timestamp: [Object],
    Double: [Object],
    MinKey: [Object],
    MaxKey: [Object],
    promoteLongs: true },
 _state: 'connected',
 pkFactory: 
  { [Function: ObjectID]
    index: 14533369,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] },
 forceServerObjectId: false,
 safe: false,
 notReplied: {},
 isInitializing: true,
 openCalled: true,
 commands: [],
 logger: { error: [Function], log: [Function], debug: [Function] },
 tag: 1426645764322,
 eventHandlers: 
  { error: [],
    parseError: [],
    poolReady: [],
    message: [],
    close: [] },
 serializeFunctions: false,
 raw: false,
 recordQueryStats: false,
 retryMiliSeconds: 1000,
 numberOfRetries: 60,
 readPreference: { _type: 'ReadPreference', mode: 'primary', tags:     undefined } },
collection: 
{ db: 
  { domain: null,
    _events: {},
    _maxListeners: 10,
    databaseName: 'askr',
    serverConfig: [Object],
    options: [Object],
    _applicationClosed: false,
    slaveOk: false,
    bufferMaxEntries: -1,
    native_parser: true,
    bsonLib: [Object],
    bson: [Object],
    bson_deserializer: [Object],
    bson_serializer: [Object],
    _state: 'connected',
    pkFactory: [Object],
    forceServerObjectId: false,
    safe: false,
    notReplied: {},
    isInitializing: true,
    openCalled: true,
    commands: [],
    logger: [Object],
    tag: 1426645764322,
    eventHandlers: [Object],
    serializeFunctions: false,
    raw: false,
    recordQueryStats: false,
    retryMiliSeconds: 1000,
    numberOfRetries: 60,
    readPreference: [Object] },
 collectionName: 'users',
 internalHint: null,
 opts: {},
 slaveOk: false,
 serializeFunctions: false,
 raw: false,
 readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
 pkFactory: 
  { [Function: ObjectID]
    index: 14533369,
    createPk: [Function: createPk],
    createFromTime: [Function: createFromTime],
    createFromHexString: [Function: createFromHexString],
    isValid: [Function: isValid],
    ObjectID: [Circular],
    ObjectId: [Circular] },
 serverCapabilities: undefined },
 selector: {},
 fields: undefined,
 skipValue: 0,
 limitValue: 0,
 sortValue: undefined,
 hint: null,
 explainValue: undefined,
 snapshot: undefined,
 timeout: true,
 tailable: undefined,
 awaitdata: undefined,
 oplogReplay: undefined,
 numberOfRetries: 5,
 currentNumberOfRetries: 5,
 batchSizeValue: 0,
 raw: false,
 readPreference: { _type: 'ReadPreference', mode: 'primary', tags: undefined },
 returnKey: undefined,
 maxScan: undefined,
 min: undefined,
 max: undefined,
 showDiskLoc: undefined,
 comment: undefined,
 tailableRetryInterval: 100,
 exhaust: false,
 partial: false,
 slaveOk: false,
 maxTimeMSValue: undefined,
 connection: undefined,
 transforms: undefined,
 totalNumberOfRecords: 0,
 items: [],
 cursorId: { _bsontype: 'Long', low_: 0, high_: 0 },
 dbName: undefined,
 state: 0,
 queryRun: false,
 getMoreTimer: false,
 collectionName: 'askr.users' }

3 个答案:

答案 0 :(得分:1)

您可以简单地使用:

collection.find().toArray();

答案 1 :(得分:0)

返回this,即collection变量。这样你就可以进行连锁电话:

Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);

参见示例here

答案 2 :(得分:0)

db.collection.find返回一个Cursor,它是 指向查询结果集的指针。客户端可以遍历游标以检索结果。默认情况下,游标在不活动10分钟后会超时

参考:http://docs.mongodb.org/manual/reference/method/db.collection.find

如果你想存储find()的结果,你可以迭代它。 例如。

collection= db.collection('users');
result =collection.find().forEach(function(item){
//here item is record. ie. what you have to do with each record.
})