Class RunCommandCursor

Hierarchy

Properties

command: Readonly<Record<string, any>>
getMoreOptions: {
    batchSize?: number;
    comment?: any;
    maxAwaitTimeMS?: number;
} = {}

Type declaration

  • Optional batchSize?: number
  • Optional comment?: any
  • Optional maxAwaitTimeMS?: number
CLOSE: "close" = ...

Event

captureRejectionSymbol: typeof captureRejectionSymbol
captureRejections: boolean

Sets or gets the default captureRejection value for all emitters.设置或获取所有发射器的默认captureRejection值。

defaultMaxListeners: number
errorMonitor: typeof errorMonitor

This symbol shall be used to install a listener for only monitoring 'error' events. Listeners installed using this symbol are called before the regular 'error' listeners are called.此符号应用于安装仅用于监视'error'事件的侦听器。在调用常规'error'侦听器之前,将调用使用此符号安装的侦听器。

Installing a listener using this symbol does not change the behavior once an 'error' event is emitted, therefore the process will still crash if no regular 'error' listener is installed.一旦发出'error'事件,使用此符号安装侦听器不会改变行为,因此,如果未安装常规的'error'侦听器,进程仍将崩溃。

Accessors

Methods

  • Unsupported for RunCommandCursor: various cursor flags must be configured directly on command documentRunCommandCursor不支持:必须直接在命令文档上配置各种游标标志

    Parameters

    • _: string
    • __: boolean

    Returns never

  • Unsupported for RunCommandCursor: batchSize must be configured directly on command documentRunCommandCursor不支持:batchSize必须直接在命令文档上配置

    Parameters

    • _: number

    Returns never

  • Iterates over all the documents for this cursor using the iterator, callback pattern.使用迭代器、回调模式对该游标的所有文档进行迭代。

    If the iterator returns false, iteration will stop.如果迭代器返回false,迭代将停止。

    Parameters

    • iterator: ((doc) => boolean | void)

      The iteration callback.

        • (doc): boolean | void
        • Parameters

          • doc: any

          Returns boolean | void

    Returns Promise<void>

    Deprecated

    • Will be removed in a future release. 将在将来的版本中删除。Use for await...of instead.请改用for await ... of
  • Map all documents using the provided function If there is a transform set on the cursor, that will be called first and the result passed to this function's transform.使用提供的函数映射所有文档如果游标上有一个转换集,将首先调用该转换集,并将结果传递给该函数的转换。

    Type Parameters

    • T = any

    Parameters

    • transform: ((doc) => T)

      The mapping transformation method.映射转换方法。

        • (doc): T
        • Parameters

          • doc: any

          Returns T

    Returns AbstractCursor<T, AbstractCursorEvents>

    Remarks

    Note Cursors use null internally to indicate that there are no more documents in the cursor. Providing a mapping function that maps values to null will result in the cursor closing itself before it has finished iterating all documents. 游标在内部使用null表示游标中不再有文档。提供一个将值映射为null的映射函数将导致游标在迭代完所有文档之前关闭。This will not result in a memory leak, just surprising behavior. For example:这不会导致内存泄漏,只是令人惊讶的行为。例如:

    const cursor = collection.find({});
    cursor.map(() => null);

    const documents = await cursor.toArray();
    // documents is always [], regardless of how many documents are in the collection.

    Other falsey values are allowed:允许其他伪值:

    const cursor = collection.find({});
    cursor.map(() => '');

    const documents = await cursor.toArray();
    // documents is now an array of empty strings

    Note for Typescript Users:Typescript用户注意事项: adding a transform changes the return type of the iteration of this cursor, it does not return a new instance of a cursor. 添加转换会更改此游标迭代的返回类型,它不会返回游标的新实例。This means when calling map, you should always assign the result to a new variable in order to get a correctly typed cursor variable. Take note of the following example:这意味着在调用map时,应该始终将结果分配给一个新变量,以便获得正确类型的游标变量。请注意以下示例:

    Example

    const cursor: FindCursor<Document> = coll.find();
    const mappedCursor: FindCursor<number> = cursor.map(doc => Object.keys(doc).length);
    const keyCounts: number[] = await mappedCursor.toArray(); // cursor.toArray() still returns Document[]
  • Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command documentRunCommandCursor不支持:必须直接在命令文档上配置maxTimeMS

    Parameters

    • _: number

    Returns never

  • Get the next available document from the cursor, returns null if no more documents are available.从游标中获取下一个可用文档,如果没有更多可用文档,则返回null

    Returns Promise<any>

  • Rewind this cursor to its uninitialized state. Any options that are present on the cursor will remain in effect. Iterating this cursor will cause new queries to be sent to the server, even if the resultant data has already been retrieved by this cursor.将此游标回退到未初始化状态。游标上的任何选项都将保持有效。迭代该游标将导致向服务器发送新的查询,即使该游标已经检索到结果数据。

    Returns void

  • Controls the getMore.maxTimeMS field. Only valid when cursor is tailable await控制getMore.maxTimeMS字段。仅当游标可裁剪等待时有效

    Parameters

    • maxTimeMS: number

      the number of milliseconds to wait for new data等待新数据的毫秒数

    Returns RunCommandCursor

  • Returns an array of documents. The caller is responsible for making sure that there is enough memory to store the results. Note that the array only contains partial results when this cursor had been previously accessed. 返回一个文档数组。调用者负责确保有足够的内存来存储结果。请注意,当以前访问过该游标时,数组只包含部分结果。In that case, cursor.rewind() can be used to reset the cursor.在这种情况下,可以使用cursor.rewind()来重置游标。

    Returns Promise<any[]>

  • Try to get the next available document from the cursor or null if an empty batch is returned尝试从游标获取下一个可用文档,如果返回空批,则为null

    Returns Promise<any>

  • Returns a copy of the array of listeners for the event named eventName.返回名为eventName的事件的侦听器数组的副本。

    For EventEmitters this behaves exactly the same as calling .listeners on the emitter.对于EventEmitter,其行为与在发射器上调用侦听器完全相同。

    For EventTargets this is the only way to get the event listeners for the event target. This is useful for debugging and diagnostic purposes.对于EventTarget,这是获取事件目标的事件侦听器的唯一方法。这对于调试和诊断非常有用。

    import { getEventListeners, EventEmitter } from 'node:events';

    {
    const ee = new EventEmitter();
    const listener = () => console.log('Events are fun');
    ee.on('foo', listener);
    console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
    }
    {
    const et = new EventTarget();
    const listener = () => console.log('Events are fun');
    et.addEventListener('foo', listener);
    console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
    }

    Parameters

    • emitter: EventEmitter | _DOMEventTarget
    • name: string | symbol

    Returns Function[]

    Since

    v15.2.0, v14.17.0

  • A class method that returns the number of listeners for the given eventNameregistered on the given emitter.一个类方法,用于返回在给定emitter上注册的给定eventName的侦听器数。

    import { EventEmitter, listenerCount } from 'node:events';

    const myEmitter = new EventEmitter();
    myEmitter.on('event', () => {});
    myEmitter.on('event', () => {});
    console.log(listenerCount(myEmitter, 'event'));
    // Prints: 2

    Parameters

    • emitter: EventEmitter

      The emitter to query要查询的发射器

    • eventName: string | symbol

      The event name

    Returns number

    Since

    v0.9.12

    Deprecated

    >Since v3.2.0 - Use listenerCount instead.>由于v3.2.0-请改用listenerCount

  • import { on, EventEmitter } from 'node:events';
    import process from 'node:process';

    const ee = new EventEmitter();

    // Emit later on
    process.nextTick(() => {
    ee.emit('foo', 'bar');
    ee.emit('foo', 42);
    });

    for await (const event of on(ee, 'foo')) {
    // The execution of this inner block is synchronous and it
    // processes one event at a time (even with await). Do not use
    // if concurrent execution is required.
    console.log(event); // prints ['bar'] [42]
    }
    // Unreachable here

    Returns an AsyncIterator that iterates eventName events. 返回一个迭代eventName事件的AsyncIteratorIt will throw if the EventEmitter emits 'error'. 如果EventEmitter发出'error',它将抛出。It removes all listeners when exiting the loop. The value returned by each iteration is an array composed of the emitted event arguments.它在退出循环时删除所有侦听器。每次迭代返回的值是一个由发出的事件参数组成的数组。

    An AbortSignal can be used to cancel waiting on events:AbortSignal可用于取消等待事件:

    import { on, EventEmitter } from 'node:events';
    import process from 'node:process';

    const ac = new AbortController();

    (async () => {
    const ee = new EventEmitter();

    // Emit later on
    process.nextTick(() => {
    ee.emit('foo', 'bar');
    ee.emit('foo', 42);
    });

    for await (const event of on(ee, 'foo', { signal: ac.signal })) {
    // The execution of this inner block is synchronous and it
    // processes one event at a time (even with await). Do not use
    // if concurrent execution is required.
    console.log(event); // prints ['bar'] [42]
    }
    // Unreachable here
    })();

    process.nextTick(() => ac.abort());

    Parameters

    • emitter: EventEmitter
    • eventName: string

      The name of the event being listened for

    • Optional options: StaticEventEmitterOptions

    Returns AsyncIterableIterator<any>

    that iterates eventName events emitted by the emitter

    Since

    v13.6.0, v12.16.0

  • Creates a Promise that is fulfilled when the EventEmitter emits the given event or that is rejected if the EventEmitter emits 'error' while waiting. 创建一个Promise,该PromiseEventEmitter发出给定事件时实现,或者如果EventEmitter在等待时发出'error'则被拒绝。The Promise will resolve with an array of all the arguments emitted to the given event.Promise将通过向给定事件发出的所有参数的数组进行解析。

    This method is intentionally generic and works with the web platform EventTarget interface, which has no special'error' event semantics and does not listen to the 'error' event.此方法是有意通用的,可与web平台EventTarget接口配合使用,该接口没有特殊的'error'事件语义,也不侦听'error'消息。

    import { once, EventEmitter } from 'node:events';
    import process from 'node:process';

    const ee = new EventEmitter();

    process.nextTick(() => {
    ee.emit('myevent', 42);
    });

    const [value] = await once(ee, 'myevent');
    console.log(value);

    const err = new Error('kaboom');
    process.nextTick(() => {
    ee.emit('error', err);
    });

    try {
    await once(ee, 'myevent');
    } catch (err) {
    console.error('error happened', err);
    }

    The special handling of the 'error' event is only used when events.once()is used to wait for another event. If events.once() is used to wait for the 'error' event itself, then it is treated as any other kind of event without special handling:'error'事件的特殊处理仅在events.once()用于等待另一个事件时使用。如果events.once()用于等待'error'事件本身,那么它将被视为任何其他类型的事件,而无需特殊处理:

    import { EventEmitter, once } from 'node:events';

    const ee = new EventEmitter();

    once(ee, 'error')
    .then(([err]) => console.log('ok', err.message))
    .catch((err) => console.error('error', err.message));

    ee.emit('error', new Error('boom'));

    // Prints: ok boom

    An AbortSignal can be used to cancel waiting for the event:AbortSignal可用于取消等待事件:

    import { EventEmitter, once } from 'node:events';

    const ee = new EventEmitter();
    const ac = new AbortController();

    async function foo(emitter, event, signal) {
    try {
    await once(emitter, event, { signal });
    console.log('event emitted!');
    } catch (error) {
    if (error.name === 'AbortError') {
    console.error('Waiting for the event was canceled!');
    } else {
    console.error('There was an error', error.message);
    }
    }
    }

    foo(ee, 'foo', ac.signal);
    ac.abort(); // Abort waiting for the event
    ee.emit('foo'); // Prints: Waiting for the event was canceled!

    Parameters

    • emitter: _NodeEventTarget
    • eventName: string | symbol
    • Optional options: StaticEventEmitterOptions

    Returns Promise<any[]>

    Since

    v11.13.0, v10.16.0

  • Parameters

    • emitter: _DOMEventTarget
    • eventName: string
    • Optional options: StaticEventEmitterOptions

    Returns Promise<any[]>

  • import { setMaxListeners, EventEmitter } from 'node:events';

    const target = new EventTarget();
    const emitter = new EventEmitter();

    setMaxListeners(5, target, emitter);

    Parameters

    • Optional n: number

      A non-negative number. The maximum number of listeners per EventTarget event.一个非负数。每个EventTarget事件的最大侦听器数。

    • Rest ...eventTargets: (EventEmitter | _DOMEventTarget)[]

    Returns void

    Since

    v15.4.0

Generated using TypeDoc