无法从数据库检索多个值



数据库中存在以下数据:

[
{
"_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
}
]

如果您取出单个值,例如,通过命令:

.findOne({id: 1});

没有问题,一切都很好。
当从基中获取多个值时会出现问题—例如:

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

这样的命令产生如下输出:

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

问题:
为什么会发生这种情况,如何获得所需的结果?

find返回值在nodejs中是一个游标,有几种方法可以使用它来访问数据,你可以在这里阅读它们

最简单的方法是使用.toArray()cursor函数,它将光标转换为文档数组,如下所示:
const results = await db.collection.find({ticker:"EURUSD"})).toArray();

最新更新