On this page本页内容
cursor.forEach(function)
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Iterates the cursor to apply a JavaScript 循环游标以将JavaScript函数应用于游标中的每个文档。function
to each document from the cursor.
The forEach()
method has the following prototype form:forEach()
方法具有以下原型形式:
db.collection.find().forEach(<function>)
The forEach()
method has the following parameter:forEach()
方法具有以下参数:
function | JavaScript | <function> signature includes a single argument that is passed the current document to process. <function> 签名包含一个参数,该参数传递给当前文档进行处理。 |
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 ); } );
cursor.map()
for similar functionality.用于类似功能。