Mikhail Krivosheev
Mikhail Krivosheev

Reputation: 569

Unable to retrieve multiple values from database

The following data exists in the database:

[
    {
        "_id": {
            "$oid": "628c787de53612aad30021ab"
        },
        "ticker": "EURUSD",
        "dtyyyymmdd": "20030505",
        "time": "030000",
        "open": "1.12161",
        "high": "1.12209",
        "low": "1.12161",
        "close": "1.12209",
        "vol": "561",
        "id": 1
    },
    {
        "_id": {
            "$oid": "628c787de53612aad30021ac"
        },
        "ticker": "EURUSD",
        "dtyyyymmdd": "20030505",
        "time": "030100",
        "open": "1.12206",
        "high": "1.1225",
        "low": "1.12206",
        "close": "1.1225",
        "vol": "1223",
        "id": 2
    },
    {
        "_id": {
            "$oid": "628c787de53612aad30021ad"
        },
        "ticker": "EURUSD",
        "dtyyyymmdd": "20030505",
        "time": "030200",
        "open": "1.12238",
        "high": "1.12247",
        "low": "1.12225",
        "close": "1.12231",
        "vol": "816",
        "id": 3
    }
]

If you pull out a single value, for example, through the command:

.findOne({id: 1});

There are no problems and everything works fine.
The problem occurs when getting multiple values from the base - for example:

.find({ticker:"EURUSD"}));

Such a command produces the following output:

FindCursor {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false,
  [Symbol(topology)]: Topology {
    _events: [Object: null prototype] {
      connectionPoolCreated: [Function (anonymous)],
      connectionPoolClosed: [Function (anonymous)],
      connectionCreated: [Function (anonymous)],
      connectionReady: [Function (anonymous)],
      connectionClosed: [Function (anonymous)],
      connectionCheckOutStarted: [Function (anonymous)],
      connectionCheckOutFailed: [Function (anonymous)],
      connectionCheckedOut: [Function (anonymous)],
      connectionCheckedIn: [Function (anonymous)],
      connectionPoolCleared: [Function (anonymous)],
      commandStarted: [Function (anonymous)],
      commandSucceeded: [Function (anonymous)],
      commandFailed: [Function (anonymous)],
      serverOpening: [Function (anonymous)],
      serverClosed: [Function (anonymous)],
      serverDescriptionChanged: [Function (anonymous)],
      topologyOpening: [Function (anonymous)],
      topologyClosed: [Function (anonymous)],
      topologyDescriptionChanged: [Function (anonymous)],
      error: [Function (anonymous)],
      timeout: [Function (anonymous)],
      close: [Function (anonymous)],
      serverHeartbeatStarted: [Function (anonymous)],
      serverHeartbeatSucceeded: [Function (anonymous)],
      serverHeartbeatFailed: [Function (anonymous)]
    },
    _eventsCount: 25,
    _maxListeners: undefined,
    bson: [Object: null prototype] {
      serialize: [Function: serialize],
      deserialize: [Function: deserialize]
    },
    s: {
      id: 0,
      options: [Object: null prototype],
      seedlist: [Array],
      state: 'connected',
      description: [TopologyDescription],
      serverSelectionTimeoutMS: 30000,
      heartbeatFrequencyMS: 10000,
      minHeartbeatFrequencyMS: 500,
      servers: [Map],
      sessionPool: [ServerSessionPool],
      sessions: Set(0) {},
      credentials: undefined,
      clusterTime: undefined,
      connectionTimers: Set(0) {},
      detectShardedTopology: [Function: detectShardedTopology],
      detectSrvRecords: [Function: detectSrvRecords]
    },
    [Symbol(kCapture)]: false,
    [Symbol(waitQueue)]: Denque {
      _head: 2,
      _tail: 2,
      _capacity: undefined,
      _capacityMask: 3,
      _list: [Array]
    }
  },
  [Symbol(namespace)]: MongoDBNamespace { db: 'Trading', collection: 'EUR/USD' },
  [Symbol(documents)]: [],
  [Symbol(initialized)]: false,
  [Symbol(closed)]: false,
  [Symbol(killed)]: false,
  [Symbol(options)]: {
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined,
      maxStalenessSeconds: undefined,
      minWireVersion: undefined
    },
    fieldsAsRaw: {},
    promoteValues: true,
    promoteBuffers: false,
    promoteLongs: true,
    serializeFunctions: false,
    ignoreUndefined: false,
    bsonRegExp: false,
    raw: false,
    enableUtf8Validation: true
  },
  [Symbol(filter)]: { ticker: 'EURUSD' },
  [Symbol(builtOptions)]: {
    raw: false,
    promoteLongs: true,
    promoteValues: true,
    promoteBuffers: false,
    ignoreUndefined: false,
    bsonRegExp: false,
    serializeFunctions: false,
    fieldsAsRaw: {},
    enableUtf8Validation: true,
    readPreference: ReadPreference {
      mode: 'primary',
      tags: undefined,
      hedge: undefined,
      maxStalenessSeconds: undefined,
      minWireVersion: undefined
    }
  }
}

Question:
Why is this happening and how to get the needed result?

Upvotes: 1

Views: 37

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22296

find return value is a cursor in nodejs, there are several ways on how you can use it to access the data, you can read about them here

The easiest way is to use the .toArray() cursor function which just converts the cursor to an array of documents, like so:

const results = await db.collection.find({ticker:"EURUSD"})).toArray();

Upvotes: 1

Related Questions