文档菜单
文档首页
/ / /
C#/.NET
/

BSON 操作

本页内容

  • 概述
  • BSON 数据格式
  • 创建 BSON 文档
  • 修改 BSON 文档
  • 将 BSON 写入文件
  • 从文件读取 BSON
  • API 文档

在本指南中,您可以学习如何使用 .NET/C# 驱动器创建 BSON 文档,从文件读取 BSON,以及将 BSON 写入文件。

BSON(二进制 JSON),是 MongoDB 用于组织和存储数据的格式。此数据格式包括所有 JSON 数据结构类型,并添加了对日期、不同大小的整数、ObjectId 和二进制数据等类型的支持。有关支持类型的完整列表,请参阅BSON 类型服务器手册页面。

本指南中的代码示例使用以下 BSON 文档作为示例

{
"address" : {
"street" : "Pizza St",
"zipcode" : "10003"
},
"coord" : [-73.982419, 41.579505]
"cuisine" : "Pizza",
"name" : "Mongo's Pizza"
}

在C#中构建BSON文档,创建一个BsonDocument类的实例。BsonDocument构造函数接受映射到文档中的字段和值的BsonElement参数。每个BsonElement可以是BsonElement类的实例,也可以是花括号内的字段-值对({})。

以下代码示例展示了如何创建一个BsonDocument对象来表示示例BSON文档。BsonDocument对象中的每个键值对都是一个BsonElement对象。

var newRestaurant = new BsonDocument
{
{ "address", new BsonDocument
{
{ "street", "Pizza St" },
{ "zipcode", "10003" }
}
},
{ "coord", new BsonArray
{-73.982419, 41.579505 }
},
{ "cuisine", "Pizza" },
{ "name", "Mongo's Pizza"}
};

BsonDocument类包含允许您更改BSON文档内容的方法。以下代码示例对之前的BsonDocument对象进行了三次更改

  1. 添加了一个新字段"restaurant_id",其值为"12345"

  2. 删除了"cuisine"字段

  3. "name"字段的值设置为"Mongo's Pizza Palace"

var newRestaurant = new BsonDocument
{
{ "address", new BsonDocument
{
{ "street", "Pizza St" },
{ "zipcode", "10003" }
}
},
{ "coord", new BsonArray
{-73.982419, 41.579505 }
},
{ "cuisine", "Pizza" },
{ "name", "Mongo's Pizza"}
};
newRestaurant.Add(new BsonElement("restaurant_id", "12345"));
newRestaurant.Remove("cuisine");
newRestaurant.Set("name", "Mongo's Pizza Palace");

注意

有关BsonDocument类中所有方法的完整列表,请参阅API文档。

您可以使用 BsonBinaryWriter 类中的方法将BSON写入文件。要写入文件,请执行以下步骤

  1. 为包含BSON数据的文件打开文件流。

  2. 使用文件流创建一个 BsonBinaryWriter

  3. 对于您想创建的每个BSON文档和子文档,调用 WriteStartDocument()

  4. 在BSON文档和子文档内部,调用 WriteName() 来设置字段名,并使用相应的 Write* 方法来设置其值。每种数据类型都有专门的 Write* 方法,您应该使用它。

  5. 要开始和结束数组,使用 WriteStartArray()WriteEndArray()

  6. 在文档和子文档结束时,调用 WriteEndDocument()

以下代码示例展示了如何将示例BSON文档写入到 myFile.bson

string outputFileName = "myFile.bson";
using (var stream = File.OpenWrite(outputFileName))
using (var writer = new BsonBinaryWriter(stream))
{
writer.WriteStartDocument();
//address
writer.WriteName("address");
writer.WriteStartDocument();
writer.WriteName("street");
writer.WriteString("Pizza St");
writer.WriteName("zipcode");
writer.WriteString("10003");
writer.WriteEndDocument();
//coord
writer.WriteName("coord");
writer.WriteStartArray();
writer.WriteDouble(-73.982419);
writer.WriteDouble(41.579505);
writer.WriteEndArray();
//cuisine
writer.WriteName("cuisine");
writer.WriteString("Pizza");
//name
writer.WriteName("name");
writer.WriteString("Mongo's Pizza");
writer.WriteEndDocument();
}

生成的BSON文档看起来如下所示

{
"address" : {
"street" : "Pizza St",
"zipcode" : "10003"
},
"coord" : [-73.982419, 41.579505]
"cuisine" : "Pizza",
"name" : "Mongo's Pizza"
}

要从文件读取BSON文档,遵循与写入BSON文档到文件相同的步骤,但有两个不同之处

  • 使用 BsonBinaryReader 而不是 BsonBinaryWriter

  • 使用 Read* 方法而不是 Write* 方法。这些方法从BSON文档返回字段名和值。

以下代码示例展示了如何从存储在 myFile.bson 中的示例BSON文档中读取字段和值

string inputFileName = "myFile.bson";
using (var stream = File.OpenRead(inputFileName))
using (var reader = new BsonBinaryReader(stream))
{
reader.ReadStartDocument();
//address
string addressFieldName = reader.ReadName();
reader.ReadStartDocument();
string streetFieldName = reader.ReadName();
string streetValue = reader.ReadString();
string zipFieldName = reader.ReadName();
string zipValue = reader.ReadString();
reader.ReadEndDocument();
//coord
string coordFieldName = reader.ReadName();
reader.ReadStartArray();
double coord1 = reader.ReadDouble();
double coord2 = reader.ReadDouble();
reader.ReadEndArray();
//cuisine
string cuisineFieldName = reader.ReadName();
string cuisineValue = reader.ReadString();
//name
string nameFieldName = reader.ReadName();
string nameValue = reader.ReadString();
reader.ReadEndDocument();
}

警告

如果你连续两次调用 ReadName() 而没有读取值,驱动程序将抛出一个 InvalidOperationException

提示

BsonBinaryReaderBsonBinaryWriter 构造函数接受任何 System.IO.Stream 对象。这意味着你可以读取或写入任何可以通过流访问的位置。

要了解更多关于本指南中讨论的任何方法或类型的信息,请参阅以下 API 文档

返回

LINQ