정의
Mongo.setReadPref(mode, tagSet)Call the
setReadPref()method on aMongoconnection object to control how the client will route all queries to members of the replica set. [1]참고
You must call
Mongo.setReadPref()on the connection object before retrieving documents using that connection to use that read preference.
| [1] | 특정 쿼리(1개 또는 여러 개)에 대해 읽기 설정을 적용하려면 반복하기 전에 커서에 cursor.readPref()를 적용하면 됩니다. 자세한 내용은 cursor.readPref()를 참조하세요. |
매개변수
Parameter | 유형 | 설명 |
|---|---|---|
문자열 | 다음 읽기 설정 (read preference) 모드 중 하나: | |
문서 배열 |
Mongo.setReadPref() does not support the Read Preference maxStalenessSeconds option for read preference.
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
행동
Mongo.setReadPref() 의 동작은 다음과 같습니다.
db.runCommand()와의 상호작용
mongosh 2.0부터 db.runCommand()는 연결 문자열을 통해 또는 Mongo.setReadPref() 메서드를 사용하여 추가된 모든 전역 읽기 설정을 무시합니다. 원하는 읽기 설정을 지정하려면 db.runCommand()를 호출할 때 options 인수를 사용합니다.
예시
읽기 설정 (read preference) 모드 지정
다음 작업에서는 읽기 설정 모드를 설정하여 읽기 대상을 세컨더리 노드로 지정합니다. 이는 암시적으로 세컨더리에서 읽기를 허용합니다.
db.getMongo().setReadPref('secondary')
그러나 mongosh를 사용하여 프라이머리에 연결된 상태에서 호출하는 경우 위 명령은 쿼리를 세컨더리로 라우팅하지 않습니다.
읽기 설정 (read preference) 태그 세트 지정
To target secondaries with specific tags, include both the mode and the tagSet array:
db.getMongo().setReadPref( "secondary", [ { "datacenter": "B" }, // First, try matching by the datacenter tag { "region": "West"}, // If not found, then try matching by the region tag { } // If not found, then use the empty document to match all eligible members ] )
세컨더리 선택 프로세스에서 MongoDB는 datacenter: "B" 태그가 있는 세컨더리 멤버를 먼저 찾으려고 합니다.
발견되면 MongoDB는 적격 세컨더리를
datacenter: "B"태그가 있는 세컨더리로 제한하고 나머지 태그를 무시합니다.아무것도 발견되지 않으면 MongoDB는
"region": "West"태그를 사용하여 세컨더리 멤버를 찾으려고 시도합니다.발견되면 MongoDB는 적격 세컨더리를
"region": "West"태그가 있는 세컨더리로 제한합니다.아무것도 발견되지 않으면 MongoDB는 적격 세컨더리를 사용합니다.
자세한 내용 은 태그 일치 순서를 참조하세요.