Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:[email protected]: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' }
like image 649
vishal patel Avatar asked Mar 18 '15 02:03

vishal patel


People also ask

What is the return type of find () in MongoDB?

In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.

What is the purpose of Find () method in MongoDB?

The find() method in MongoDB selects documents in a collection or view and returns a cursor to the selected documents. It has two parameters: query and projection.

What does MongoDB find return if not found?

The . findOne() method of the base driver similarly returns either a result or a null document where the conditions do not match.

What is the type of MongoDB DB database?

MongoDB is a document database.


1 Answers

You can simply use:

collection.find().toArray();
like image 153
E. Karim Avatar answered Oct 23 '22 14:10

E. Karim