cursor.forEach()

On this page本页内容

Description描述

cursor.forEach(function)

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

Iterates the cursor to apply a JavaScript function to each document from the cursor.迭代游标,将JavaScript函数应用于游标中的每个文档。

The forEach() method has the following prototype form:forEach()方法的原型形式如下:

db.collection.find().forEach(<function>)

The forEach() method has the following parameter:forEach()方法具有以下参数:

Parameter参数Type类型Description描述
function JavaScript A JavaScript function to apply to each document from the cursor. 一个JavaScript函数,用于从游标应用到每个文档。The <function> signature includes a single argument that is passed the current document to process.<function>签名包含一个参数,该参数通过当前文档传递给进程。

Example示例

The following example invokes the forEach() method on the cursor returned by find() to print the name of each user in the collection:以下示例对find()返回的游标调用forEach()方法,以打印集合中每个用户的名称:

db.users.find().forEach( function(myDoc) { print( "user: " + myDoc.name ); } );

See also参阅

cursor.map() for similar functionality.用于类似的功能。