개요
이 가이드에서는 .NET/C# 드라이버를 사용하여 MongoDB 데이터베이스 및 컬렉션에 액세스하고 관리하는 방법을 배울 수 있습니다.
MongoDB는 데이터를 계층적 구조로 구성합니다. MongoDB 배포서버에는 하나 이상의 데이터베이스 가 포함되며, 각 데이터베이스에는 하나 이상의 collection 이 포함됩니다. 각 collection에서 MongoDB는 필드 및 값 쌍을 포함하는 문서 로 데이터를 저장합니다.
문서 데이터 형식에 학습 보려면 서버 매뉴얼의 문서 를 참조하세요.
데이터베이스에 액세스
You can access a database by retrieving an IMongoDatabase instance from your IMongoClient
instance. You can use the returned IMongoDatabase
instance to perform database-level operations and access collections that the database contains.
를 생성하려면 IMongoDatabase
IMongoClient 인스턴스 에서 GetDatabase() 메서드를 호출하고 데이터베이스 이름을 매개 변수로 전달합니다. 선택 사항인 MongoDatabaseSettings를 매개변수로 전달하여 데이터베이스 액세스 방법을 사용자 지정할 수도 있습니다.
존재하지 않는 데이터베이스의 이름을 GetDatabase()
메서드에 전달하면 드라이버는 여전히 IMongoDatabase
인스턴스를 반환합니다. 이 데이터베이스의 컬렉션에 데이터를 삽입하면 서버가 해당 시점에 데이터베이스와 컬렉션을 생성합니다.
다음 예에서는 클라이언트를 만든 다음 GetDatabase()
메서드를 사용하여 test_db
데이터베이스에 액세스합니다.
var client = new MongoClient("<connection string>"); var myDB = mongoClient.GetDatabase("test_db");
목록 데이터베이스
To see a list of your deployment's databases, call the asynchronous ListDatabaseNamesAsync() method or synchronous ListDatabaseNames() method on your IMongoClient
instance.
To see detailed information about each database, call the asynchronous ListDatabasesAsync() method or synchronous ListDatabases() method on your IMongoClient
instance. These methods return fields describing the databases in the cluster, such as their sizes and whether they contain data.
다음 코드는 비동기 ListDatabaseNamesAsync()
메서드 또는 동기 ListDatabaseNames()
메서드를 사용하여 클러스터의 데이터베이스 이름을 나열하는 방법을 보여줍니다.
await mongoClient.ListDatabaseNamesAsync();
mongoClient.ListDatabaseNames();
데이터베이스 제거
Dropping a database permanently deletes all the data in that database's collections. To drop a database, call the asynchronous DropDatabaseAsync() method or synchronous DropDatabase() method on your IMongoClient
instance, passing the database name as the parameter.
다음 코드는 비동기 DropDatabaseAsync()
메서드 또는 동기 DropDatabase()
메서드를 사용하여 test_db
데이터베이스를 삭제하는 방법을 보여줍니다.
await mongoClient.DropDatabaseAsync("test_db");
mongoClient.DropDatabase("test_db");
경고
데이터베이스를 삭제하면 데이터가 삭제됩니다.
데이터베이스를 삭제하면 데이터베이스의 모든 collection의 모든 문서와 해당 collection의 모든 인덱스가 영구적으로 삭제됩니다. 데이터베이스를 삭제한 후에는 해당 데이터에 액세스하거나 복원할 수 없습니다.
컬렉션에 액세스
IMongoCollection
데이터베이스 IMongoCollection
에서 IMongoCollection 인스턴스 검색하여 컬렉션 액세스 할 수 있습니다. 인스턴스 사용하여 데이터 작업을 수행하고, 애그리게이션을 생성하고, 인덱스를 관리 수 있습니다. 를 조회 하려면 인스턴스 에서 GetCollection() 메서드를 호출합니다.IMongoDatabase
선택 사항인 MongoCollectionSettings를 매개 변수로 전달하여 컬렉션 액세스 방법을 사용자 지정할 수도 있습니다.
존재하지 않는 컬렉션의 이름을 이 메서드에 전달하면 드라이버는 여전히 IMongoCollection
인스턴스를 반환합니다. 이 컬렉션에 데이터를 삽입하면 서버가 해당 컬렉션을 생성합니다. 컬렉션을 명시적으로 만드는 방법을 알아보려면 이 가이드 의 컬렉션 만들기 섹션을 참조하세요.
이 예에서는 GetCollection()
메서드를 사용하여 myDB
변수가 참조하는 데이터베이스에서 coll_xyz
컬렉션에 액세스합니다.
var myColl = myDB.GetCollection<BsonDocument>("coll_xyz");
collection 매개변수화
collection의 데이터를 직렬화할 데이터 유형을 지정하여 IMongoCollection
인스턴스를 매개변수화해야 합니다. 특정 유형으로 매개변수화된 IMongoCollection
인스턴스에서 메서드를 호출하면 메서드는 이 유형의 인스턴스를 허용하거나 반환합니다.
다음 예시에서는 BsonDocument
유형으로 컬렉션을 매개변수화하는 방법을 보여줍니다.
var collection = database.GetCollection<BsonDocument>("coll_xyz", settings);
팁
BsonDocument
유형 대신 데이터를 모델링하는 사용자 지정 유형으로 IMongoCollection
인스턴스를 매개변수화하는 것이 좋습니다. 특정 데이터를 모델링하는 유형을 정의하여 반복적인 직렬화 및 유효성 검사를 방지할 수 있습니다.
.NET/ C# 드라이버 의 직렬화에 학습 보려면 직렬화가이드 를 참조하세요.
컬렉션 생성
You can explicitly create a collection by calling the asynchronous CreateCollectionAsync() method or synchronous CreateCollection() method on your IMongoDatabase
instance.
이 메서드는 컬렉션 이름과 선택적 CreateCollectionOptions 유형을 매개 변수로 사용합니다. 그런 다음 생성된 컬렉션 액세스 데이터 작업을 수행하고, 애그리게이션을 생성하고, 인덱스를 관리 수 있습니다.
다음 코드는 비동기 CreateCollectionAsync()
메서드 또는 동기 CreateCollection()
메서드를 사용하여 myDB
변수가 참조하는 데이터베이스 내에서 coll_abc
컬렉션을 만드는 방법을 보여줍니다.
await myDB.CreateCollectionAsync("coll_abc");
myDB.CreateCollection("coll_abc");
listCollections (영문)
To see a list of collections in a database, call the asynchronous ListCollectionNamesAsync() method or synchronous ListCollectionNames() method on your IMongoDatabase
instance.
To see detailed information about each database, call the asynchronous ListCollectionsAsync() method or synchronous ListCollections() method on your IMongoDatabase
instance. These methods return fields describing the collections in the database, such as their types and settings.
다음 코드는 비동기 ListCollectionNamesAsync()
메서드 또는 동기 ListCollectionNames()
메서드를 사용하여 데이터베이스의 컬렉션 이름을 나열하는 방법을 보여줍니다.
await myDB.ListCollectionNamesAsync();
myDB.ListCollectionNames();
제거 collection
Dropping a collection permanently deletes all the data in that collection. To drop a collection, call the asynchronous DropCollectionAsync() method or the synchronous DropCollection() method on your IMongoCollection
instance.
다음 코드는 비동기 DropCollectionAsync()
메서드 또는 동기 DropCollection()
메서드를 사용하여 coll_abc
데이터베이스를 삭제하는 방법을 보여줍니다.
await myDB.DropCollectionAsync("coll_abc");
myDB.DropCollection("coll_abc");
경고
collection을 삭제하면 데이터가 삭제됩니다.
데이터베이스에서 컬렉션을 삭제하면 해당 컬렉션 내의 모든 문서와 해당 컬렉션의 모든 인덱스가 영구적으로 삭제됩니다. collection을 삭제한 후에는 collection의 데이터에 액세스하거나 복원할 수 없습니다.
추가 정보
이 가이드의 개념에 대한 자세한 내용은 다음 문서를 참조하세요.
문서 삽입 가이드
MongoDB Server 매뉴얼의 데이터베이스 및 컬렉션
MongoDB Server 매뉴얼의 문서