node.js - 如何使用来自不同 Node.js 的 Mongoose 获取聚合搜索值?
问题描述
我有这个猫鼬查询来获取一些 geoNear 值。
const results = await mongoose.connection.db
.collection('placemodels')
.aggregate([
{
$geoNear: {
near: { type: 'Point', coordinates: [89.9741, 21.5112] },
distanceField: 'calcDistance',
maxDistance: 25000,
spherical: true,
},
},
]);
console.log(results);
但是,我收到
AggregationCursor {
_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: false,
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: AggregateOperation {
options: { readPreference: [ReadPreference] },
ns: MongoDBNamespace { db: 'test', collection: '$cmd' },
readPreference: ReadPreference { mode: 'primary', tags: undefined },
readConcern: undefined,
writeConcern: WriteConcern { w: 'majority' },
explain: false,
fullResponse: true,
target: 'placemodels',
pipeline: [ [Object] ],
hasWriteStage: false,
cursorState: {
cursorId: null,
cmd: {},
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: 'test.$cmd',
namespace: MongoDBNamespace { db: 'test', collection: '$cmd' },
cmd: {},
options: {
readPreference: ReadPreference { mode: 'primary', tags: undefined }
},
topology: NativeTopology {
_events: [Object: null prototype] {
topologyDescriptionChanged: [Array],
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: [Function (anonymous)],
serverHeartbeatFailed: [Function (anonymous)],
topologyOpening: [Function (anonymous)],
topologyClosed: [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: [MongoCredentials],
clusterTime: [Object],
connectionTimers: Set(0) {},
srvPoller: [SrvPoller],
detectTopologyDescriptionChange: [Function (anonymous)]
},
[Symbol(kCapture)]: false,
[Symbol(waitQueue)]: Denque { _head: 0, _tail: 0, _capacityMask: 3, _list: [Array] }
},
cursorState: {
cursorId: null,
cmd: {},
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
}
这个结果?
我尝试使用不同的查询,如 .find() 和其他东西。似乎没有任何效果。如何从聚合搜索中获取价值?我也尝试在内部进行查询collection('model', (err, collections) => {---- HERE ----})
,但没有成功:(
解决方案
您需要使用来自 的数据AggregationCursor
。一种方法是:
for (let doc = await results.next(); doc != null; doc = await results.next()) {
console.log(doc);
// process your aggregation result here
}
有关游标的更多详细信息,请参阅文档。
推荐阅读
- python - 如何在 python beautiful soup 中提取大括号内的值?
- javascript - 在 Angular 5 中显示一个对象
- javascript - 替换 Javascript 字符串中的单引号支持 UTF-8
- c# - Type.GetType().GetProperties 未返回具有好友访问修饰符的属性
- flask - 由于不允许的预检标头,Vue.js 前端与 Flask 后端 CORS 问题交互
- php - 从codeigniter中的复选框获取空值
- html - 复选框检查烧瓶
- javascript - React 和 Three.js
- node.js - 如何在 Loopback 中使用 EXISTS 子查询?
- c# - 对项目进行分组并删除其中一些