What is return type of db.collection.find() in mon

2020-06-23 08:23发布

问题:

I am trying to read the output of find function from mongodb connection.

I want to store result of database to JSON/Array called result like :

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

Right now with this code in result i am getting some weird long json output:

{ 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' }

回答1:

db.collection.find returns a Cursor which is A pointer to the result set of a query. Clients can iterate through a cursor to retrieve results. By default, cursors timeout after 10 minutes of inactivity

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

if you want to store result of find() you can iterate over it . eg.

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


回答2:

You can simply use:

collection.find().toArray();


回答3:

It returns this, i.e. the collection variable back. So that you can do chain calls:

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

See examples here.