AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

커서.forEach() (mongosh 메서드)

cursor.forEach(function)

중요

Mongo쉬 방법

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

MongoDB API 드라이버의 경우 언어별 MongoDB 드라이버 설명서를 참조하세요.

커서를 같은 동작으로 여러 번 움직여서 커서가 가리키는 각 문서에 JavaScript function을(를) 적용하세요.

이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

참고

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

이 메서드에는 다음과 같은 구문이 있습니다.

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

이 메서드는 다음 필드를 허용합니다.

필드
유형
설명

function

JavaScript 코드

커서에서 반환된 각 문서에 적용하는 함수입니다. 함수 서명에는 커서에서 읽은 현재 문서를 저장하는 하나의 필드가 포함되어 있습니다.

users 컬렉션을 생성합니다.

db.users.insertMany( [
{ name: "John" },
{ name: "Jane" }
] )

다음 예시에서는 forEach()을(를) find() 메서드와 함께 사용하여 users collection에서 읽은 사용자 이름을 인쇄합니다. myDoc은(는) 현재 문서를 저장합니다.

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

출력 예시:

User name: John
User name: Jane

Starting in mongosh 2.1.0, you can also use for-of loops. The following example returns the same results as the previous example:

for ( const myDoc of db.users.find() ) {
print( "User name: " + myDoc.name )
}

유사한 기능을 가진 메서드에 대한 자세한 내용은 cursor.map()를 참조하세요.