MongoDB——BsonDocument
BsonElement
(Bson元素)
Bson元素是一个name/value的键值对。
document.Add(new BsonElement("age", 21)); // OK, but next line is shorter 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函数
BsonDocument book = new BsonDocument(); book.Add("author", "Ernest Hemingway"); book.Add("title", "For Whom the Bell Tolls");
另外一种方式
BsonDocument book = new BsonDocument() .Add("author", "Ernest Hemingway") .Add("title", "For Whom the Bell Tolls");
使用c#的collection初始化语法(推荐)
BsonDocument book = new BsonDocument { { "author", "Ernest Hemingway" }, { "title", "For Whom the Bell Tolls" } }; 编译器会将它翻译成调用Add函数的方式
BsonDocument book = new BsonDocument(); book.Add("author", "Ernest Hemingway"); book.Add("title", "For Whom the Bell Tolls"); 不要缺少里面的大括号,不然就会像下面一样
BsonDocument bad = new BsonDocument { "author", "Ernest Hemingway" }; 编译成:
BsonDocument bad = new BsonDocument(); bad.Add("author"); bad.Add("Ernest Hemingway");
创建嵌套的BSON documents
BsonDocument nested = new BsonDocument { { "name", "John Doe" }, { "address", new BsonDocument { { "street", "123 Main St." }, { "city", "Centerville" }, { "state", "PA" }, { "zip", 12345} }} }; ”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中
BsonDocument document = new BsonDocument { { "name", name }, { "city", city }, // not added if city is null { "dob", dob, dobAvailable } // not added if dobAvailable is false }; 可以翻译成如下:
BsonDocument document = new BsonDocument(); document.Add("name", name); if (city != null) { document.Add("city", city); } if (dobAvailable) { document.Add("dob", dob); } 如果想add一个BsonNull值,可以使用C#中的null联合
BsonDocument = new BsonDocument { { "city", city ?? BsonConstants.Null } };
获取BsonDocument的元素elements
- BsonValue this[int index]
- BsonValue this[string name]
- BsonValue this[string name, BsonValue defaultValue]
返回的是BsonValue值,例子如下
BsonDocument book; string author = book["author"].AsString; DateTime publicationDate = book["publicationDate"].AsDateTime; 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)
// traditional approach BsonArray a1 = new BsonArray(); a1.Add(1); a2.Add(2); // fluent interface BsonArray a2 = new BsonArray().Add(1).Add(2); // values argument int[] values = new int[] { 1, 2 }; BsonArray a3 = new BsonArray(values); // collection initializer syntax BsonArray a4 = new BsonArray { 1, 2 };
访问
BsonArray array = new BsonArray { "Tom", 39 }; string name = array[0].AsString; int age = array[1].AsInt32;
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: MongoDB——地理空间索引和查询
- 下一篇: web前端——session和cookie