db.getSiblingDB()
정의
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
참고
이 명령은 모든 MongoDB Atlas 클러스터에서 지원됩니다. 모든 명령에 대한 Atlas 지원에 관해 자세히 알아보려면 지원되지 않는 명령을 참조하십시오.
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.
Consider a MongoDB instance with two databases, users
and
records
. The active
collection is a part of the users
database. The requests
collection is a part of the records
database.
Specify a Database
This operation sets the db
object to point to the database named
users
, and then returns a document count
for the active
collection.
db = db.getSiblingDB('users') db.active.countDocuments()
Use Multiple Databases
You can create multiple db
objects, that refer to different
databases, as in the following sequence of operations:
users = db.getSiblingDB('users') records = db.getSiblingDB('records') users.active.countDocuments() users.active.findOne() records.requests.countDocuments() records.requests.findOne()
This operation creates two db
objects. Each db
object refers to
a different database, users
or records
.
For each database, the query returns:
a
document count
, and
from a collection in that database.