Class BsonDocument
Represents a BSON document.
[Serializable]
public class BsonDocument : BsonValue, IComparable<BsonValue>, IConvertible, IEquatable<BsonValue>, IComparable<BsonDocument>, IConvertibleToBsonDocument, IEnumerable<BsonElement>, IEnumerable, IEquatable<BsonDocument>
- Inheritance
-
BsonDocument
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
BsonDocument()
Initializes a new instance of the BsonDocument class.
public BsonDocument()
BsonDocument(BsonElement)
Initializes a new instance of the BsonDocument class and adds one element.
public BsonDocument(BsonElement element)
Parameters
elementBsonElementAn element to add to the document.
BsonDocument(params BsonElement[])
Initializes a new instance of the BsonDocument class and adds one or more elements.
[Obsolete("Use BsonDocument(IEnumerable<BsonElement> elements) instead.")]
public BsonDocument(params BsonElement[] elements)
Parameters
elementsBsonElement[]One or more elements to add to the document.
BsonDocument(bool)
Initializes a new instance of the BsonDocument class specifying whether duplicate element names are allowed (allowing duplicate element names is not recommended).
public BsonDocument(bool allowDuplicateNames)
Parameters
allowDuplicateNamesboolWhether duplicate element names are allowed.
BsonDocument(Dictionary<string, object>)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
public BsonDocument(Dictionary<string, object> dictionary)
Parameters
dictionaryDictionary<string, object>A dictionary to initialize the document from.
BsonDocument(Dictionary<string, object>, IEnumerable<string>)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
[Obsolete("Use BsonDocument(IEnumerable<BsonElement> elements) instead.")]
public BsonDocument(Dictionary<string, object> dictionary, IEnumerable<string> keys)
Parameters
dictionaryDictionary<string, object>A dictionary to initialize the document from.
keysIEnumerable<string>A list of keys to select values from the dictionary.
BsonDocument(IDictionary<string, object>, IEnumerable<string>)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
[Obsolete("Use BsonDocument(IEnumerable<BsonElement> elements) instead.")]
public BsonDocument(IDictionary<string, object> dictionary, IEnumerable<string> keys)
Parameters
dictionaryIDictionary<string, object>A dictionary to initialize the document from.
keysIEnumerable<string>A list of keys to select values from the dictionary.
BsonDocument(IEnumerable<BsonElement>)
Initializes a new instance of the BsonDocument class and adds new elements from a list of elements.
public BsonDocument(IEnumerable<BsonElement> elements)
Parameters
elementsIEnumerable<BsonElement>A list of elements to add to the document.
BsonDocument(IEnumerable<KeyValuePair<string, object>>)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
public BsonDocument(IEnumerable<KeyValuePair<string, object>> dictionary)
Parameters
dictionaryIEnumerable<KeyValuePair<string, object>>A dictionary to initialize the document from.
BsonDocument(IDictionary)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
public BsonDocument(IDictionary dictionary)
Parameters
dictionaryIDictionaryA dictionary to initialize the document from.
BsonDocument(IDictionary, IEnumerable)
Initializes a new instance of the BsonDocument class and adds new elements from a dictionary of key/value pairs.
[Obsolete("Use BsonDocument(IEnumerable<BsonElement> elements) instead.")]
public BsonDocument(IDictionary dictionary, IEnumerable keys)
Parameters
dictionaryIDictionaryA dictionary to initialize the document from.
keysIEnumerableA list of keys to select values from the dictionary.
BsonDocument(string, BsonValue)
Initializes a new instance of the BsonDocument class and creates and adds a new element.
public BsonDocument(string name, BsonValue value)
Parameters
namestringThe name of the element to add to the document.
valueBsonValueThe value of the element to add to the document.
Properties
AllowDuplicateNames
Gets or sets whether to allow duplicate names (allowing duplicate names is not recommended).
public bool AllowDuplicateNames { get; set; }
Property Value
BsonType
Gets the BsonType of this BsonValue.
public override BsonType BsonType { get; }
Property Value
ElementCount
Gets the number of elements.
public virtual int ElementCount { get; }
Property Value
Elements
Gets the elements.
public virtual IEnumerable<BsonElement> Elements { get; }
Property Value
this[int]
Gets or sets a value by position.
public override BsonValue this[int index] { get; set; }
Parameters
indexintThe position.
Property Value
- BsonValue
The value.
this[string]
Gets or sets a value by name.
public override BsonValue this[string name] { get; set; }
Parameters
namestringThe name.
Property Value
- BsonValue
The value.
this[string, BsonValue]
Gets the value of an element or a default value if the element is not found.
[Obsolete("Use GetValue(string name, BsonValue defaultValue) instead.")]
public virtual BsonValue this[string name, BsonValue defaultValue] { get; }
Parameters
namestringThe name of the element.
defaultValueBsonValueThe default value to return if the element is not found.
Property Value
- BsonValue
Teh value of the element or a default value if the element is not found.
Names
Gets the element names.
public virtual IEnumerable<string> Names { get; }
Property Value
RawValues
Gets the raw values (see BsonValue.RawValue).
[Obsolete("Use Values instead.")]
public virtual IEnumerable<object> RawValues { get; }
Property Value
Values
Gets the values.
public virtual IEnumerable<BsonValue> Values { get; }
Property Value
Methods
Add(BsonElement)
Adds an element to the document.
public virtual BsonDocument Add(BsonElement element)
Parameters
elementBsonElementThe element to add.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(params BsonElement[])
Adds a list of elements to the document.
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public virtual BsonDocument Add(params BsonElement[] elements)
Parameters
elementsBsonElement[]The list of elements.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(Dictionary<string, object>)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange instead.")]
public virtual BsonDocument Add(Dictionary<string, object> dictionary)
Parameters
dictionaryDictionary<string, object>The dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(Dictionary<string, object>, IEnumerable<string>)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public virtual BsonDocument Add(Dictionary<string, object> dictionary, IEnumerable<string> keys)
Parameters
dictionaryDictionary<string, object>The dictionary.
keysIEnumerable<string>Which keys of the hash table to add.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(IDictionary<string, object>)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange instead.")]
public virtual BsonDocument Add(IDictionary<string, object> dictionary)
Parameters
dictionaryIDictionary<string, object>The dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(IDictionary<string, object>, IEnumerable<string>)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public virtual BsonDocument Add(IDictionary<string, object> dictionary, IEnumerable<string> keys)
Parameters
dictionaryIDictionary<string, object>The dictionary.
keysIEnumerable<string>Which keys of the hash table to add.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(IEnumerable<BsonElement>)
Adds a list of elements to the document.
[Obsolete("Use AddRange instead.")]
public virtual BsonDocument Add(IEnumerable<BsonElement> elements)
Parameters
elementsIEnumerable<BsonElement>The list of elements.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(IDictionary)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange instead.")]
public virtual BsonDocument Add(IDictionary dictionary)
Parameters
dictionaryIDictionaryThe dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(IDictionary, IEnumerable)
Adds elements to the document from a dictionary of key/value pairs.
[Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
public virtual BsonDocument Add(IDictionary dictionary, IEnumerable keys)
Parameters
dictionaryIDictionaryThe dictionary.
keysIEnumerableWhich keys of the hash table to add.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(string, BsonValue)
Creates and adds an element to the document.
public virtual BsonDocument Add(string name, BsonValue value)
Parameters
Returns
- BsonDocument
The document (so method calls can be chained).
Add(string, BsonValue, bool)
Creates and adds an element to the document, but only if the condition is true.
public virtual BsonDocument Add(string name, BsonValue value, bool condition)
Parameters
namestringThe name of the element.
valueBsonValueThe value of the element.
conditionboolWhether to add the element to the document.
Returns
- BsonDocument
The document (so method calls can be chained).
Add(string, Func<BsonValue>, bool)
Creates and adds an element to the document, but only if the condition is true. If the condition is false the value factory is not called at all.
public virtual BsonDocument Add(string name, Func<BsonValue> valueFactory, bool condition)
Parameters
namestringThe name of the element.
valueFactoryFunc<BsonValue>A delegate called to compute the value of the element if condition is true.
conditionboolWhether to add the element to the document.
Returns
- BsonDocument
The document (so method calls can be chained).
AddRange(Dictionary<string, object>)
Adds elements to the document from a dictionary of key/value pairs.
public virtual BsonDocument AddRange(Dictionary<string, object> dictionary)
Parameters
dictionaryDictionary<string, object>The dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
AddRange(IEnumerable<BsonElement>)
Adds a list of elements to the document.
public virtual BsonDocument AddRange(IEnumerable<BsonElement> elements)
Parameters
elementsIEnumerable<BsonElement>The list of elements.
Returns
- BsonDocument
The document (so method calls can be chained).
AddRange(IEnumerable<KeyValuePair<string, object>>)
Adds elements to the document from a dictionary of key/value pairs.
public virtual BsonDocument AddRange(IEnumerable<KeyValuePair<string, object>> dictionary)
Parameters
dictionaryIEnumerable<KeyValuePair<string, object>>The dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
AddRange(IDictionary)
Adds elements to the document from a dictionary of key/value pairs.
public virtual BsonDocument AddRange(IDictionary dictionary)
Parameters
dictionaryIDictionaryThe dictionary.
Returns
- BsonDocument
The document (so method calls can be chained).
Clear()
Clears the document (removes all elements).
public virtual void Clear()
Clone()
Creates a shallow clone of the document (see also DeepClone).
public override BsonValue Clone()
Returns
- BsonValue
A shallow clone of the document.
CompareTo(BsonDocument)
Compares this document to another document.
public virtual int CompareTo(BsonDocument rhs)
Parameters
rhsBsonDocumentThe other document.
Returns
- int
A 32-bit signed integer that indicates whether this document is less than, equal to, or greather than the other.
CompareTo(BsonValue)
Compares the BsonDocument to another BsonValue.
public override int CompareTo(BsonValue other)
Parameters
otherBsonValueThe other BsonValue.
Returns
- int
A 32-bit signed integer that indicates whether this BsonDocument is less than, equal to, or greather than the other BsonValue.
Contains(string)
Tests whether the document contains an element with the specified name.
public virtual bool Contains(string name)
Parameters
namestringThe name of the element to look for.
Returns
- bool
True if the document contains an element with the specified name.
ContainsValue(BsonValue)
Tests whether the document contains an element with the specified value.
public virtual bool ContainsValue(BsonValue value)
Parameters
valueBsonValueThe value of the element to look for.
Returns
- bool
True if the document contains an element with the specified value.
Create(object)
Creates a new BsonDocument by mapping an object to a BsonDocument.
public static BsonDocument Create(object value)
Parameters
valueobjectThe object to be mapped to a BsonDocument.
Returns
- BsonDocument
A BsonDocument.
DeepClone()
Creates a deep clone of the document (see also Clone).
public override BsonValue DeepClone()
Returns
- BsonValue
A deep clone of the document.
Equals(BsonDocument)
Compares this document to another document.
public bool Equals(BsonDocument obj)
Parameters
objBsonDocumentThe other document.
Returns
- bool
True if the two documents are equal.
Equals(object)
Compares this BsonDocument to another object.
public override bool Equals(object obj)
Parameters
objobjectThe other object.
Returns
- bool
True if the other object is a BsonDocument and equal to this one.
GetElement(int)
Gets an element of this document.
public virtual BsonElement GetElement(int index)
Parameters
indexintThe zero based index of the element.
Returns
- BsonElement
The element.
GetElement(string)
Gets an element of this document.
public virtual BsonElement GetElement(string name)
Parameters
namestringThe name of the element.
Returns
- BsonElement
A BsonElement.
GetEnumerator()
Gets an enumerator that can be used to enumerate the elements of this document.
public virtual IEnumerator<BsonElement> GetEnumerator()
Returns
- IEnumerator<BsonElement>
An enumerator.
GetHashCode()
Gets the hash code.
public override int GetHashCode()
Returns
- int
The hash code.
GetValue(int)
Gets the value of an element.
public virtual BsonValue GetValue(int index)
Parameters
indexintThe zero based index of the element.
Returns
- BsonValue
The value of the element.
GetValue(string)
Gets the value of an element.
public virtual BsonValue GetValue(string name)
Parameters
namestringThe name of the element.
Returns
- BsonValue
The value of the element.
GetValue(string, BsonValue)
Gets the value of an element or a default value if the element is not found.
public virtual BsonValue GetValue(string name, BsonValue defaultValue)
Parameters
namestringThe name of the element.
defaultValueBsonValueThe default value returned if the element is not found.
Returns
- BsonValue
The value of the element or the default value if the element is not found.
IndexOfName(string)
Gets the index of an element.
public virtual int IndexOfName(string name)
Parameters
namestringThe name of the element.
Returns
- int
The index of the element, or -1 if the element is not found.
InsertAt(int, BsonElement)
Inserts a new element at a specified position.
public virtual void InsertAt(int index, BsonElement element)
Parameters
indexintThe position of the new element.
elementBsonElementThe element.
Merge(BsonDocument)
Merges another document into this one. Existing elements are not overwritten.
public virtual BsonDocument Merge(BsonDocument document)
Parameters
documentBsonDocumentThe other document.
Returns
- BsonDocument
The document (so method calls can be chained).
Merge(BsonDocument, bool)
Merges another document into this one, specifying whether existing elements are overwritten.
public virtual BsonDocument Merge(BsonDocument document, bool overwriteExistingElements)
Parameters
documentBsonDocumentThe other document.
overwriteExistingElementsboolWhether to overwrite existing elements.
Returns
- BsonDocument
The document (so method calls can be chained).
Parse(string)
Parses a JSON string and returns a BsonDocument.
public static BsonDocument Parse(string json)
Parameters
jsonstringThe JSON string.
Returns
- BsonDocument
A BsonDocument.
Remove(string)
Removes an element from this document (if duplicate element names are allowed then all elements with this name will be removed).
public virtual void Remove(string name)
Parameters
namestringThe name of the element to remove.
RemoveAt(int)
Removes an element from this document.
public virtual void RemoveAt(int index)
Parameters
indexintThe zero based index of the element to remove.
RemoveElement(BsonElement)
Removes an element from this document.
public virtual void RemoveElement(BsonElement element)
Parameters
elementBsonElementThe element to remove.
Set(int, BsonValue)
Sets the value of an element.
public virtual BsonDocument Set(int index, BsonValue value)
Parameters
indexintThe zero based index of the element whose value is to be set.
valueBsonValueThe new value.
Returns
- BsonDocument
The document (so method calls can be chained).
Set(string, BsonValue)
Sets the value of an element (an element will be added if no element with this name is found).
public virtual BsonDocument Set(string name, BsonValue value)
Parameters
Returns
- BsonDocument
The document (so method calls can be chained).
SetElement(BsonElement)
Sets an element of the document (replaces any existing element with the same name or adds a new element if an element with the same name is not found).
public virtual BsonDocument SetElement(BsonElement element)
Parameters
elementBsonElementThe new element.
Returns
- BsonDocument
The document.
SetElement(int, BsonElement)
Sets an element of the document (replacing the existing element at that position).
public virtual BsonDocument SetElement(int index, BsonElement element)
Parameters
indexintThe zero based index of the element to replace.
elementBsonElementThe new element.
Returns
- BsonDocument
The document.
ToDictionary()
Converts the BsonDocument to a Dictionary<string, object>.
public Dictionary<string, object> ToDictionary()
Returns
- Dictionary<string, object>
A dictionary.
ToHashtable()
Converts the BsonDocument to a Hashtable.
public Hashtable ToHashtable()
Returns
- Hashtable
A hashtable.
ToString()
Returns a string representation of the document.
public override string ToString()
Returns
- string
A string representation of the document.
TryGetElement(string, out BsonElement)
Tries to get an element of this document.
public virtual bool TryGetElement(string name, out BsonElement value)
Parameters
namestringThe name of the element.
valueBsonElementThe element.
Returns
- bool
True if an element with that name was found.
TryGetValue(string, out BsonValue)
Tries to get the value of an element of this document.
public virtual bool TryGetValue(string name, out BsonValue value)
Parameters
Returns
- bool
True if an element with that name was found.
TryParse(string, out BsonDocument)
Tries to parse a JSON string and returns a value indicating whether it succeeded or failed.
public static bool TryParse(string s, out BsonDocument result)
Parameters
sstringThe JSON string.
resultBsonDocumentThe result.
Returns
- bool
Whether it succeeded or failed.
Operators
operator ==(BsonDocument, BsonDocument)
Compares two BsonDocument values.
public static bool operator ==(BsonDocument lhs, BsonDocument rhs)
Parameters
lhsBsonDocumentThe first BsonDocument.
rhsBsonDocumentThe other BsonDocument.
Returns
- bool
True if the two BsonDocument values are equal according to ==.
operator !=(BsonDocument, BsonDocument)
Compares two BsonDocument values.
public static bool operator !=(BsonDocument lhs, BsonDocument rhs)
Parameters
lhsBsonDocumentThe first BsonDocument.
rhsBsonDocumentThe other BsonDocument.
Returns
- bool
True if the two BsonDocument values are not equal according to ==.