Table of Contents

Class BsonDocument

Namespace
MongoDB.Bson
Assembly
MongoDB.Bson.dll

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

element BsonElement

An 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

elements BsonElement[]

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

allowDuplicateNames bool

Whether 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

dictionary Dictionary<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

dictionary Dictionary<string, object>

A dictionary to initialize the document from.

keys IEnumerable<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

dictionary IDictionary<string, object>

A dictionary to initialize the document from.

keys IEnumerable<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

elements IEnumerable<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

dictionary IEnumerable<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

dictionary IDictionary

A 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

dictionary IDictionary

A dictionary to initialize the document from.

keys IEnumerable

A 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

name string

The name of the element to add to the document.

value BsonValue

The 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

bool

BsonType

Gets the BsonType of this BsonValue.

public override BsonType BsonType { get; }

Property Value

BsonType

ElementCount

Gets the number of elements.

public virtual int ElementCount { get; }

Property Value

int

Elements

Gets the elements.

public virtual IEnumerable<BsonElement> Elements { get; }

Property Value

IEnumerable<BsonElement>

this[int]

Gets or sets a value by position.

public override BsonValue this[int index] { get; set; }

Parameters

index int

The position.

Property Value

BsonValue

The value.

this[string]

Gets or sets a value by name.

public override BsonValue this[string name] { get; set; }

Parameters

name string

The 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

name string

The name of the element.

defaultValue BsonValue

The 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

IEnumerable<string>

RawValues

Gets the raw values (see BsonValue.RawValue).

[Obsolete("Use Values instead.")]
public virtual IEnumerable<object> RawValues { get; }

Property Value

IEnumerable<object>

Values

Gets the values.

public virtual IEnumerable<BsonValue> Values { get; }

Property Value

IEnumerable<BsonValue>

Methods

Add(BsonElement)

Adds an element to the document.

public virtual BsonDocument Add(BsonElement element)

Parameters

element BsonElement

The 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

elements BsonElement[]

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

dictionary Dictionary<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

dictionary Dictionary<string, object>

The dictionary.

keys IEnumerable<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

dictionary IDictionary<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

dictionary IDictionary<string, object>

The dictionary.

keys IEnumerable<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

elements IEnumerable<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

dictionary IDictionary

The 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

dictionary IDictionary

The dictionary.

keys IEnumerable

Which 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

name string

The name of the element.

value BsonValue

The value of the element.

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

name string

The name of the element.

value BsonValue

The value of the element.

condition bool

Whether 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

name string

The name of the element.

valueFactory Func<BsonValue>

A delegate called to compute the value of the element if condition is true.

condition bool

Whether 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

dictionary Dictionary<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

elements IEnumerable<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

dictionary IEnumerable<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

dictionary IDictionary

The 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

rhs BsonDocument

The 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

other BsonValue

The 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

name string

The 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

value BsonValue

The 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

value object

The 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

obj BsonDocument

The 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

obj object

The 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

index int

The 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

name string

The 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

index int

The 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

name string

The 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

name string

The name of the element.

defaultValue BsonValue

The 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

name string

The 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

index int

The position of the new element.

element BsonElement

The element.

Merge(BsonDocument)

Merges another document into this one. Existing elements are not overwritten.

public virtual BsonDocument Merge(BsonDocument document)

Parameters

document BsonDocument

The 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

document BsonDocument

The other document.

overwriteExistingElements bool

Whether 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

json string

The 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

name string

The name of the element to remove.

RemoveAt(int)

Removes an element from this document.

public virtual void RemoveAt(int index)

Parameters

index int

The zero based index of the element to remove.

RemoveElement(BsonElement)

Removes an element from this document.

public virtual void RemoveElement(BsonElement element)

Parameters

element BsonElement

The element to remove.

Set(int, BsonValue)

Sets the value of an element.

public virtual BsonDocument Set(int index, BsonValue value)

Parameters

index int

The zero based index of the element whose value is to be set.

value BsonValue

The 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

name string

The name of the element whose value is to be set.

value BsonValue

The new value.

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

element BsonElement

The 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

index int

The zero based index of the element to replace.

element BsonElement

The 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

name string

The name of the element.

value BsonElement

The 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

name string

The name of the element.

value BsonValue

The value of the element.

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

s string

The JSON string.

result BsonDocument

The 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

lhs BsonDocument

The first BsonDocument.

rhs BsonDocument

The 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

lhs BsonDocument

The first BsonDocument.

rhs BsonDocument

The other BsonDocument.

Returns

bool

True if the two BsonDocument values are not equal according to ==.