Document 文件类的常用方法

版权声明: https://blog.csdn.net/weixin_41722928/article/details/86367747

BsonElement

(Bson元素)
  
  
Bson元素是一个name/value的键值对。
  
  

  
  
  1. document.Add( new BsonElement( "age", 21)); // OK, but next line is shorter
  2. document.Add( "age", 21); // creates BsonElement automatically

  
  

BsonDocument

 
  
  
BsonDocument是name/value键值对的集合。
  
  
BsonDocument构造函数
  
  
  • BsonDocument()
  • BsonDocument(string name, BsonValue value)

上面是用的比较多

  • BsonDocument(BsonElement element)
  • BsonDocument(Dictionary<string, object> dictionary)
  • BsonDocument(Dictionary<string, object> dictionary, IEnumerable<string> keys)
  • BsonDocument(IDictionary dictionary)
  • BsonDocument(IDictionary dictionary, IEnumerable<string> keys)
  • BsonDocument(IDictionary<string, object> dictionary)
  • BsonDocument(IDictionary<string, object> dictionary, IEnumerable<string> keys)
  • BsonDocument(IEnumerabe<BsonElement> elements)
  • BsonDocument(params BsonElement[] elements)
  • BsonDocument(bool allowDuplicateNames)

创建一个新的document并且调用Add和Set函数


  
  
  1. BsonDocument book = new BsonDocument();
  2. book.Add( "author", "Ernest Hemingway");
  3. book.Add( "title", "For Whom the Bell Tolls");
另外一种方式
  
  

  
  
  1. BsonDocument book = new BsonDocument()
  2. .Add( "author", "Ernest Hemingway")
  3. .Add( "title", "For Whom the Bell Tolls");
使用c#的collection初始化语法(推荐)
  
  

  
  
  1. BsonDocument book = new BsonDocument {
  2. { "author", "Ernest Hemingway" },
  3. { "title", "For Whom the Bell Tolls" }
  4. };
  5. 编译器会将它翻译成调用Add函数的方式

  
  
  1. BsonDocument book = new BsonDocument();
  2. book.Add( "author", "Ernest Hemingway");
  3. book.Add( "title", "For Whom the Bell Tolls");
  4. 不要缺少里面的大括号,不然就会像下面一样

  
  
  1. BsonDocument bad = new BsonDocument {
  2. "author", "Ernest Hemingway"
  3. };
  4. 编译成:

  
  
  1. BsonDocument bad = new BsonDocument();
  2. bad.Add( "author");
  3. bad.Add( "Ernest Hemingway");
创建嵌套的BSON documents
  
  

  
  
  1. BsonDocument nested = new BsonDocument {
  2. { "name", "John Doe" },
  3. { "address", new BsonDocument {
  4. { "street", "123 Main St." },
  5. { "city", "Centerville" },
  6. { "state", "PA" },
  7. { "zip", 12345}
  8. }}
  9. };
  10. ”address”是一个嵌套的document
 
  
  

ADD函数

  • Add(BsonElement element)
  • Add(Dictionary<string, object> dictionary)
  • Add(Dictionary<string, object> dictionary, IEnumerable<string> keys)
  • Add(IDictionary dictionary)
  • Add(IDictionary dictionary, IEnumerable<string> keys)
  • Add(IDictionary<string, object> dictionary)
  • Add(IDictionary<string, object> dictionary, IEnumerable<string> keys)
  • Add(IEnumerable<BsonElement> elements)
  • Add(string name, BsonValue value)
  • Add(string name, BsonValue value, bool condition)

如果里面的属性值是null的话,add函数将不把数据加入到document中


  
  
  1. BsonDocument document = new BsonDocument {
  2. { "name", name },
  3. { "city", city }, // not added if city is null
  4. { "dob", dob, dobAvailable } // not added if dobAvailable is false
  5. };
  6. 可以翻译成如下:

  
  
  1. BsonDocument document = new BsonDocument();
  2. document.Add( "name", name);
  3. if (city != null) {
  4. document.Add( "city", city);
  5. }
  6. if (dobAvailable) {
  7. document.Add( "dob", dob);
  8. }
  9. 如果想 add一个BsonNull值,可以使用C #中的null联合

  
  
  1. BsonDocument = new BsonDocument {
  2. { "city", city ?? BsonConstants.Null }
  3. };
获取BsonDocument的元素elements
  
  
  • BsonValue this[int index]
  • BsonValue this[string name]
  • BsonValue this[string name, BsonValue defaultValue]

返回的是BsonValue值,例子如下


  
  
  1. BsonDocument book;
  2. string author = book[ "author"].AsString;
  3. DateTime publicationDate = book[ "publicationDate"].AsDateTime;
  4. int pages = book[ "pages", -1].AsInt32; // default value is -1

  
  

BsonArray

这个类是用来表示BSON 数组。

构造函数

  • BsonArray()
  • BsonArray(IEnumerable<bool> values)
  • BsonArray(IEnumerable<BsonValue> values)
  • BsonArray(IEnumerable<DateTime> values)
  • BsonArray(IEnumerable<double> values)
  • BsonArray(IEnumerable<int> values)
  • BsonArray(IEnumerable<long> values)
  • BsonArray(IEnumerable<ObjectId> values)
  • BsonArray(IEnumerable<string> values)
  • BsonArray(IEnumerable values)

Add 和 AddRange 函数

  • BsonArray Add(BsonValue value)
  • BsonArray AddRange(IEnumerable<bool> values)
  • BsonArray AddRange(IEnumerable<BsonValue> values)
  • BsonArray AddRange(IEnumerable<DateTime> values)
  • BsonArray AddRange(IEnumerable<double> values)
  • BsonArray AddRange(IEnumerable<int> values)
  • BsonArray AddRange(IEnumerable<long> values)
  • BsonArray AddRange(IEnumerable<ObjectId> values)
  • BsonArray AddRange(IEnumerable<string> values)
  • BsonArray AddRange(IEnumerable values)

  
  
  1. // traditional approach
  2. BsonArray a1 = new BsonArray();
  3. a1.Add( 1);
  4. a2.Add( 2);
  5. // fluent interface
  6. BsonArray a2 = new BsonArray().Add( 1).Add( 2);
  7. // values argument
  8. int[] values = new int[] { 1, 2 };
  9. BsonArray a3 = new BsonArray(values);
  10. // collection initializer syntax
  11. BsonArray a4 = new BsonArray { 1, 2 };

 

访问


  
  
  1. BsonArray array = new BsonArray { "Tom", 39 };
  2. string name = array[ 0].AsString;
  3. int age = array[ 1].AsInt32;

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/86367747
今日推荐