Overview
在本指南中,您可以了解如何使用 .NET/C# 驱动程序访问和管理 MongoDB 数据库和集合。
MongoDB 以分层结构组织数据。 MongoDB 部署包含一个或多个数据库,每个数据库又包含一个或多个集合。 在每个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");
listDatabases
To see a list of your deployment's databases, call the synchronous ListDatabaseNames() method or asynchronous ListDatabaseNamesAsync() method on your IMongoClient instance.
To see detailed information about each database, call the synchronous ListDatabases() method or asynchronous ListDatabasesAsync() method on your IMongoClient instance. These methods return fields describing the databases in the cluster, such as their sizes and whether they contain data.
以下代码演示如何使用同步 ListDatabaseNames() 方法或异步 ListDatabaseNamesAsync() 方法列出集群中的数据库名称:
mongoClient.ListDatabaseNames();
await mongoClient.ListDatabaseNamesAsync();
删除数据库
Dropping a database permanently deletes all the data in that database's collections. To drop a database, call the synchronous DropDatabase() method or asynchronous DropDatabaseAsync() method on your IMongoClient instance, passing the database name as the parameter.
以下代码演示如何使用同步 DropDatabase() 方法或异步 DropDatabaseAsync() 方法删除名为 test_db 的数据库:
mongoClient.DropDatabase("test_db");
await mongoClient.DropDatabaseAsync("test_db");
警告
删除数据库会删除数据
删除数据库会永久删除该数据库集合中的所有文档以及这些集合上的所有索引。 删除数据库后,您将无法访问或恢复其中的任何数据。
访问集合
You can access a collection by retrieving an IMongoCollection instance from your database. You can use an IMongoCollection instance to perform data operations, create aggregations, and manage indexes. To retrieve an IMongoCollection, call the GetCollection() method on an IMongoDatabase instance. You can also pass an optional MongoCollectionSettings as a parameter to customize how you access a collection.
如果将不存在的集合名称传递给此方法,驱动程序仍会返回IMongoCollection实例。 当您将任何数据插入此集合时,服务器都会创建该数据。 要了解如何显式创建集合,请参阅本指南的创建集合部分。
此示例使用GetCollection()方法从myDB变量引用的数据库访问名为coll_xyz的集合:
var myColl = myDB.GetCollection<BsonDocument>("coll_xyz");
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 synchronous CreateCollection() method or asynchronous CreateCollectionAsync() method on your IMongoDatabase instance.
此方法将集合名称和可选的CreateCollectionOptions类型作为参数。然后,您可以访问权限创建的集合以执行数据操作、创建聚合和管理索引。
以下代码演示如何使用同步 CreateCollection() 方法或异步 CreateCollectionAsync() 方法在 myDB 变量引用的数据库中创建名为 coll_abc 的集合:
myDB.CreateCollection("coll_abc");
await myDB.CreateCollectionAsync("coll_abc");
listCollections
To see a list of collections in a database, call the synchronous ListCollectionNames() method or asynchronous ListCollectionNamesAsync() method on your IMongoDatabase instance.
To see detailed information about each database, call the synchronous ListCollections() method or asynchronous ListCollectionsAsync() method on your IMongoDatabase instance. These methods return fields describing the collections in the database, such as their types and settings.
以下代码演示如何使用同步 ListCollectionNames() 方法或异步 ListCollectionNamesAsync() 方法列出数据库中集合的名称:
myDB.ListCollectionNames();
await myDB.ListCollectionNamesAsync();
删除集合
Dropping a collection permanently deletes all the data in that collection. To drop a collection, call the synchronous DropCollection() or the asynchronous DropCollectionAsync() method on your IMongoCollection instance.
以下代码演示如何使用同步 DropCollection() 方法或异步 DropCollectionAsync() 方法删除名为 coll_abc 的数据库:
myDB.DropCollection("coll_abc");
await myDB.DropCollectionAsync("coll_abc");
警告
删除collection会删除数据
从数据库中删除集合会永久删除该集合中的所有文档以及该集合上的所有索引。 删除collection后,您将无法访问或恢复其中的任何数据。
更多信息
有关本指南中概念的更多信息,请参阅以下文档: