정의
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- 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의 소스 사용 가능 무료 자체 관리 버전
예시
You can use db.getSiblingDB() as an alternative to the use <database> helper. This is particularly useful when writing scripts using mongosh where the use helper is not available.
users 및 records라는 두 개의 데이터베이스를 가진 MongoDB 인스턴스를 예로 들어 보겠습니다. active 컬렉션은 users 데이터베이스에 포함되어 있습니다. requests 컬렉션은 records 데이터베이스에 포함되어 있습니다.
데이터베이스 지정
이 작업은 db 객체가 users라는 데이터베이스를 가리키도록 설정한 다음, activeactive 컬렉션에 대해 document count를 반환합니다.
db = db.getSiblingDB('users') db.active.countDocuments()
여러 데이터베이스 사용
다음과 같은 작업 순서와 같이 서로 다른 데이터베이스를 참조하는 db 객체를 여러 개 생성할 수 있습니다.
users = db.getSiblingDB('users') records = db.getSiblingDB('records') users.active.countDocuments() users.active.findOne() records.requests.countDocuments() records.requests.findOne()
이 작업은 두 개의 db 객체를 만듭니다. 각 db 객체는 서로 다른 데이터베이스 users 또는 records를 참조합니다.
각 데이터베이스에 대해 쿼리는 다음을 반환합니다.
해당 데이터베이스의 collection에서 반환합니다.