정의
db.getCollection(name)Returns a collection or a view object that is functionally equivalent to using the
db.<collectionName>syntax. The method is useful for a collection or a view whose name might interact withmongoshitself, such as names that begin with_or that match a database shell method.The
db.getCollection()method has the following parameter:Parameter유형설명name문자열
컬렉션의 이름입니다.
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
참고
이 명령은 모든 MongoDB Atlas 클러스터에서 지원됩니다. 모든 명령에 대한 Atlas 지원에 관해 자세히 알아보려면 지원되지 않는 명령을 참조하세요.
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
행동
The db.getCollection() object can access any collection methods.
지정된 컬렉션은 서버에 존재할 수도 있고 존재하지 않을 수도 있습니다. 컬렉션이 존재하지 않는 경우, MongoDB는 db.collection.insertOne() 과 같은 쓰기 작업의 일부로 컬렉션을 암시적으로 생성합니다.
예시
The following example uses db.getCollection() to access the auth collection and insert a document into it.
var authColl = db.getCollection("auth") authColl.insertOne( { usrName : "John Doe", usrDept : "Sales", usrTitle : "Executive Account Manager", authLevel : 4, authDept : [ "Sales", "Customers"] } )
이렇게 하면 다음이 반환됩니다.
{ "acknowledged" : true, "insertedId" : ObjectId("569525e144fe66d60b772763") }
The previous example requires the use of db.getCollection("auth") because of a name conflict with the database method db.auth(). Calling db.auth directly to perform an insert operation would reference the db.auth() method and would error.
The following example attempts the same operation, but without using the db.getCollection() method:
db.auth.insertOne( { usrName : "John Doe", usrDept : "Sales", usrTitle : "Executive Account Manager", authLevel : 4, authDept : [ "Sales", "Customers"] } )
db.auth() 메서드에 insertOne 메서드가 없어서 작업 오류가 발생했습니다.
팁