BSON Modifying#
Types#
-
struct bson_mut#
A BSON document mutator.
- C++ API:
- Zero-initialized:
A zero-initialized
bson_mut
has no defined semantics and should not be used.- Header:
A
bson_mut
is created usingbson_mutate()
or may come from another API that derives a newbson_mut
from an existingbson_mut
.Important
The mutator retains a pointer to the object from which it was created. It is essential that the parent object remain in-place while a mutator is in use.
-
class bson::mutator#
A BSON document mutator.
- C API:
Important
The mutator retains a pointer to the object from which it was created. It is essential that the parent object remain in-place while a mutator is in use.
-
iterator begin() const#
-
iterator end() const#
-
iterator find(std::string_view) const#
-
bson_byte *data() const#
-
std::uint32_t byte_size() const#
-
iterator insert(iterator pos, auto pair)#
-
iterator insert(iterator pos, bson_iterator::reference elem)#
- iterator emplace(
- iterator pos,
- std::string_view key,
- __bson_value_convertible auto val,
- C API:
- Invalidation:
All reachable iterators are invalidated
-
iterator push_back(auto pair)#
-
iterator push_back(bson_iterator::reference elem)#
-
iterator emplace_back(std::string_view key, __bson_value_convertible auto val)#
- C API:
- Invalidation:
All reachable iterators are invalidated
These are equivalent to
insert()
oremplace()
with the end iterator as the insertion position.
-
inserted_subdocument insert_subdoc(iterator pos, std::string_view key)#
-
inserted_subdocument insert_array(iterator pos, std::string_view key)#
-
mutator push_subdoc(std::string_view key)#
-
mutator push_array(std::string_view key)#
Insert a new empty child document or array, and obtain a mutator for that child.
Important
The mutator retains a pointer to the object from which it was created. It is essential that the parent object remain in-place while a mutator is in use.
Functions & Macros#
The document iteration/reading APIs detailed in BSON Iteration and BSON Reading also
work with bson_mut
objects, including:
-
bson_mut bson_mutate(bson_doc*)#
Obtain a new
bson_mut
\(M\) mutator for the given document object.- Invalidation:
No iterators are invalidated by this call, but subsequent operations on \(M\) may potentially invalidate them.
Important
The mutator retains a pointer to the object from which it was created. It is essential that the parent object remain in-place while a mutator is in use.
Inserting Data#
- [[1]] bson_iterator bson_insert(
- bson_mut *m,
- __string_convertible key,
- __bson_value_convertible value,
- [[2]] bson_iterator bson_insert(
- bson_mut *m,
- bson_iterator pos,
- __string_convertible key,
- __bson_value_convertible value,
Insert a a value into a BSON document referred-to by
m
.- Parameters:
- Returns:
Upon success, returns an iterator that refers to the inserted element. If there is an allocation failure, returns bson_end(*m).
- Invalidation:
All reachable iterators are invalidated
Note
This is implemented as a C preprocessor macro
Value Types
The type of the newly inserted value is determined according to the
__bson_value_convertible
type rules.
-
bson_mut bson_mut_child(bson_mut *parent, bson_iterator pos)#
Obtain a mutator \(M\) that manipulates a child document element at position
pos
withinparent
.- Parameters:
- Invalidation:
No iterators are invalidated by this function, but subsequent operations may invalidate them. Use
bson_mut_parent_iterator()
to recovery the iteratorpos
from \(M\).
Important
The mutator retains a pointer to the object from which it was created. It is essential that the parent object remain in-place while a mutator is in use.
-
bson_iterator bson_mut_parent_iterator(bson_mut m)#
Obtain a
bson_iterator
that refers to the position ofm
within a parent document. This can only be called on abson_mut
that was created as a child of anotherbson_mut
.- Parameters:
m – A mutator object that was returned by
bson_mut_child()
. Calling this with a mutator returnedbson_mutate()
is undefined behavior.- Invalidation:
No iterators are invalidated.
This is useful to recover an iterator referring to a child document element after mutating that child document, since mutating a child may invalidate iterators in the parent.
Removing Elements#
-
[[1]] bson_iterator bson_erase(bson_mut *m, bson_iterator pos)#
- [[2]] bson_iterator bson_erase(
- bson_mut *m,
- bson_iterator first,
- bson_iterator last,
Erase one or multiple elements within a document
m
.- Parameters:
m – A mutator for the document to be modified.
pos – A valid iterator referring to the single element to be erased.
first – The first element to be erased.
last – The first element to be retained, or the end iterator.
- Returns:
- Invalidation:
All reachable iterators are invalidated
bson_erase_range
will invalidate all reachable iterators if-and-only-iffirst
is not equal tolast
.
If
first
andlast
are equivalent, then no element will be removed.Note
This is implemented as a C preprocessor macro
Modifying Elements#
Existing document elements can be modified in-place to a limited extent.
- bson_iterator bson_set_key(
- bson_mut *mut,
- bson_iterator pos,
- __string_convertible new_key,
Replace the element key of the element pointed-to by
pos
.- Parameters:
mut – Mutator for the document owning
pos
pos – A valid iterator pointing to a live element. Must not be an error or end iterator.
new_key – The key string to be replaced.
- Returns:
The adjusted
pos
iterator following the modification.- Invalidation:
All reachable iterators are invalidated if-and-only-if
new_key
is not the same length as bson_key(pos).
- bson_iterator bson_relabel_array_elements_at(
- bson_mut *doc,
- bson_iterator pos,
- uint32_t idx,
-
void bson_relabel_array_elements(bson_mut *doc)#
Relabel elements within a BSON document
doc
as monotonically increasing decimal integers.bson_relabel_array_elements()
is equivalent to bson_relabel_array_elements_at(doc, bson_begin(*doc), 0).- Parameters:
- Returns:
Returns the iterator referring to the
pos
element after the relabelling is complete.- Invalidation:
All reachable iterators are invalidated if-and-only-if the length of any element’s new key is not equal to the length of its existing key. (When in doubt, assume all iterators are invalidated.)
Splicing Ranges#
- bson_iterator bson_splice_disjoint_ranges(
- bson_mut *m,
- bson_iterator pos,
- bson_iterator delete_end,
- bson_iterator from_begin,
- bson_iterator from_end,
Delete elements from
m
and insert elements from another document into their place.- Parameters:
m – The document being modifed.
pos – The position at which the splice operation will occur.
delete_end – The first element after
pos
which will not be deleted. If equal topos
, then no elements will be erased.from_begin – The first element to copy into
pos
from_end – The end of the range from which to copy.
- Returns:
Returns the adjusted iterator pointing to the
pos
element.- Invalidation:
All reachable iterators are invalidated if-and-only-if any elements are deleted or inserted (i.e. pos != delete_end or from_begin != from_end)
Important
If
from_begin
andfrom_end
are not equal, thenfrom_begin
andfrom_end
MUST NOT be elements withinm
or any elements within its document heirarchy.Note
delete_end
must be reachable frompos
, andfrom_end
must be reachable fromfrom_begin
.
- bson_iterator bson_insert_disjoint_range(
- bson_mut *doc,
- bson_iterator pos,
- bson_iterator from_begin,
- bson_iterator from_end,
Copy elements in the range
[from_begin, from_end)
into the documentdoc
atpos
.Equivalent to bson_splice_disjoint_ranges(doc, pos, pos, from_begin, from_end)
- Invalidation:
All reachable iterators are invalidated if-and-only-if from_begin != from_end
Utilities#
-
struct bson_u32_string#
-
char buf[11]#
A small string enough to encode a non-negative 32-bit integer with a null terminator.
-
char buf[11]#
-
bson_u32_string bson_u32_string_create(uint32_t i)#
Create a small C string representing the base-10 encoding of the given 32-bit integer
i
. The string is not dynamically allocated, so no deallocation is necessary. The character array in the returned small string is null-terminated.
Behavioral Notes#
Iterator Invalidation#
A BSON iterator \(I\) is reachable for a bson_doc
\(D\) or bson_mut
derived
from \(D\) if there is any way to obtain that iterator by traversing the document
heirarchy. \(I\) may be an iterator at the top level, or may be an iterator within
any sub-document of \(D\).
A BSON iterator \(I\) that reachable in a bson_doc
\(D\) may be invalidated by
certain operations on \(D\) or any sub-document thereof. This is true even if
the operation does not cause a reallocation! For this reason, the insertion,
erasing, and splicing APIs all return iterators that are adjusted to account for
the invalidating operations. Iterator invalidation behaviors are documented
under the Invalidation field on the relevant function.
After modifying a subdocument \(D'\) using bson_mut_child()
, an iterator referring
to \(D'\) can be recovered by using bson_mut_parent_iterator()
on the mutator that
was created with bson_mut_child()
.