* Create a new document sequence for the provided field. * @param field - The field it will replace.
(field: string, documents?: Document[])
| 448 | * @param field - The field it will replace. |
| 449 | */ |
| 450 | constructor(field: string, documents?: Document[]) { |
| 451 | this.field = field; |
| 452 | this.documents = []; |
| 453 | this.chunks = []; |
| 454 | this.serializedDocumentsLength = 0; |
| 455 | // Document sequences starts with type 1 at the first byte. |
| 456 | // Field strings must always be UTF-8. |
| 457 | const buffer = ByteUtils.allocateUnsafe(1 + 4 + this.field.length + 1); |
| 458 | buffer[0] = 1; |
| 459 | // Third part is the field name at offset 5 with trailing null byte. |
| 460 | encodeUTF8Into(buffer, `${this.field}\0`, 5); |
| 461 | this.chunks.push(buffer); |
| 462 | this.header = buffer; |
| 463 | if (documents) { |
| 464 | for (const doc of documents) { |
| 465 | this.push(doc, BSON.serialize(doc)); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Push a document to the document sequence. Will serialize the document |