정의
cursor.forEach(function)중요
Mongo쉬 방법
This page documents a
mongoshmethod. 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.
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
구문
이 메서드에는 다음과 같은 구문이 있습니다.
db.collection.find().forEach( <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()를 참조하세요.