node.js - 猫鼬:查找所有具有特定字段的文档
问题描述
我的一些文档有一个像这样的额外"Game"
字段
{
"_id" : NumberLong(204648),
"loc" : [
-41.2887953,
174.7772114
],
"ts" : "2020-09-11T02:54:11Z",
"v" : 52,
"game": 2, <--------
}
我想返回这些在"Game"
字段中具有任何值的文档。
mongoose.connection.db.collection('games').find({ "game": {} }, (err, docs) => {
console.log(docs);
});
我试过了。但是,它只是返回undefined
。我怎样才能 console.log 只记录具有该"game"
字段值的文档?
const a = await mongoose.connection.db.collection('game').find({ game: { $exists: true } });
console.log(a);
返回
Cursor {
_readableState: ReadableState {
objectMode: true,
highWaterMark: 16,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: true,
destroyed: false,
errored: null,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: false,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
operation: FindOperation {
options: {
skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: [ReadPreference],
db: [Db],
promiseLibrary: [Function: Promise]
},
ns: MongoDBNamespace { db: 'db', collection: 'game' },
cmd: {
find: 'db.game',
limit: 0,
skip: 0,
query: [Object],
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: [ReadPreference]
},
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined
},
cursorState: {
cursorId: null,
cmd: [Object],
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined,
raw: undefined
}
},
pool: null,
server: null,
disconnectHandler: undefined,
bson: BSON {},
ns: 'db.game',
namespace: MongoDBNamespace { db: 'db', collection: 'game' },
cmd: {
find: 'db.game',
limit: 0,
skip: 0,
query: { tg: [Object] },
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined
}
},
options: {
skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: ReadPreference {
mode: 'primary',
tags: undefined,
hedge: undefined
},
db: Db {
_events: [Object: null prototype],
_eventsCount: 3,
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
[Symbol(kCapture)]: false
},
promiseLibrary: [Function: Promise]
},
topology: NativeTopology {
_events: [Object: null prototype] {
authenticated: [Function (anonymous)],
error: [Array],
timeout: [Array],
close: [Array],
parseError: [Array],
fullsetup: [Array],
all: [Array],
reconnect: [Array],
commandStarted: [Function (anonymous)],
commandSucceeded: [Function (anonymous)],
commandFailed: [Function (anonymous)],
serverOpening: [Function (anonymous)],
serverClosed: [Function (anonymous)],
serverDescriptionChanged: [Function (anonymous)],
serverHeartbeatStarted: [Function (anonymous)],
serverHeartbeatSucceeded: [Array],
serverHeartbeatFailed: [Function (anonymous)],
topologyOpening: [Function (anonymous)],
topologyClosed: [Function (anonymous)],
topologyDescriptionChanged: [Function (anonymous)],
joined: [Array],
left: [Function (anonymous)],
ping: [Function (anonymous)],
ha: [Function (anonymous)],
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)],
open: [Function],
reconnectFailed: [Function (anonymous)]
},
_eventsCount: 36,
_maxListeners: Infinity,
s: {
id: 0,
options: [Object],
seedlist: [Array],
state: 'connected',
description: [TopologyDescription],
serverSelectionTimeoutMS: 30000,
heartbeatFrequencyMS: 10000,
minHeartbeatFrequencyMS: 500,
Cursor: [class Cursor extends CoreCursor],
bson: BSON {},
servers: [Map],
sessionPool: [ServerSessionPool],
sessions: Set(0) {},
promiseLibrary: [Function: Promise],
credentials: undefined,
clusterTime: null,
connectionTimers: Set(0) {},
sCapabilities: [ServerCapabilities]
},
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: Denque {
_head: 2,
_tail: 2,
_capacity: undefined,
_capacityMask: 3,
_list: [Array]
}
},
cursorState: {
cursorId: null,
cmd: {
find: 'db.game',
limit: 0,
skip: 0,
query: [Object],
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: true,
readPreference: [ReadPreference]
},
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined,
raw: undefined
},
logger: Logger { className: 'Cursor' },
s: {
numberOfRetries: 5,
tailableRetryInterval: 500,
currentNumberOfRetries: 5,
state: 0,
promiseLibrary: [Function: Promise],
explicitlyIgnoreSession: false
},
[Symbol(kCapture)]: false
}
我没有得到任何文件 console.log 但只是这个光标返回。
解决方案
推荐阅读
- typescript - 我怎样才能使类型在 firebase 云功能上工作?
- elixir - Phoenix 控制器测试用例丢失 current_user
- algorithm - 从 N 个数字列表中计算最大和,每个列表中取一个元素
- xml - YAML XML 过滤器
- c - 在C中对重复行进行排序
- android-studio - 将 Android Studio 中的类结构选项卡移动到项目文件下方
- java - 使用 liquibase 插入空值
- android - Android 6 上的表格行没有占用 100% 的宽度
- c# - 以编程方式读取脱机 EDB 文件中的 Exchange 邮箱
- html - Craft CMS - 使用插件内的 resourceUrl 无法播放音频文件