notmongo 0.1.7

In-process, bad database with an API similar to MongoDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
use crate::*;
use bson;
use bson::Bson;
use libc;
use std::ffi;

#[repr(C)]
pub struct wrapped_bson {
    pub value: *mut ffi::c_void,
    pub value_length: usize,
}

impl wrapped_bson {
    pub fn new_success(result: &Bson) -> wrapped_bson {
        wrapped_bson::from(bson::doc! {
            "success": true,
            "result": result
        })
    }

    pub fn new_success_null() -> wrapped_bson {
        wrapped_bson::new_success(&Bson::Null)
    }

    pub fn new_delete_result(result: DeleteResult) -> wrapped_bson {
        wrapped_bson::from(bson::doc! {
            "success": true,
            "result": {
                "nDeleted": result.n_deleted as i64,
            }
        })
    }
    pub fn new_update_result(result: UpdateResult) -> wrapped_bson {
        let upserted_id = match result.upserted_id {
            Some(id) => id,
            None => Bson::Null,
        };

        wrapped_bson::from(bson::doc! {
            "success": true,
            "result": {
                "matchedCount": result.matched_count as i64,
                "modifiedCount": result.modified_count as i64,
                "upsertedId": upserted_id,
            }
        })
    }
    pub fn new_insert_one_result(result: InsertOneResult) -> wrapped_bson {
        wrapped_bson::from(bson::doc! {
            "success": true,
            "result": {
                "insertedId": result.inserted_id,
            }
        })
    }
    pub fn new_insert_many_result(result: InsertManyResult) -> wrapped_bson {
        wrapped_bson::from(bson::doc! {
            "success": true,
            "result": {
                "insertedIds": Bson::Array(result.inserted_ids),
            }
        })
    }

    pub fn new_error(error_message: &str) -> wrapped_bson {
        wrapped_bson::from(bson::doc! {
            "success": false,
            "errorMessage": error_message
        })
    }

    pub fn consume_into_document(self) -> bson::Document {
        let as_slice =
            unsafe { std::slice::from_raw_parts(self.value as *mut u8, self.value_length) };

        // TODO: Why would this be able to fail?
        // Assuming the bytestream is valid that is.
        bson::Document::from_reader(as_slice).unwrap()
    }
}

impl From<bson::Document> for wrapped_bson {
    fn from(document: bson::Document) -> Self {
        unsafe {
            let bytes_vec = bson::ser::to_vec(&document).unwrap(); // Why can this fail?

            // The result HAS TO be allocated via malloc, because that allocator is
            // available to all other languages.
            let bytes_c = libc::malloc(bytes_vec.len());

            for ii in 0..bytes_vec.len() {
                *(bytes_c as *mut u8).offset(ii as isize) = bytes_vec[ii];
            }

            wrapped_bson {
                value: bytes_c,
                value_length: bytes_vec.len(),
            }
        }
    }
}

impl Drop for wrapped_bson {
    fn drop(&mut self) {
        unsafe {
            libc::free(self.value);
        }
    }
}

unsafe fn get_collection<'a>(
    db: &'a mut Database,
    collection_name: *const libc::c_char,
) -> Result<&'a mut Collection, String> {
    // Convert the string to Rust
    let collection_name = match ffi::CStr::from_ptr(collection_name).to_str() {
        Ok(s) => s,
        Err(_) => return Err("The collection name is not a valid UTF-8 C-String".to_string()),
    };

    // Try to get the collection
    Ok(db.collection_mut(collection_name))
}

unsafe fn get_filter(filter: wrapped_bson) -> Result<FindQuery, String> {
    // Convert to BSON
    let filter = filter.consume_into_document();

    // Parse
    FindQuery::parse_bson(filter)
}

unsafe fn get_update(filter: wrapped_bson) -> Result<UpdateQuery, String> {
    // Convert to BSON
    let filter = filter.consume_into_document();

    // Parse
    UpdateQuery::parse_bson(filter)
}

unsafe fn get_projection<'a>(projection: wrapped_bson) -> Result<Projection, String> {
    // Extract the actual projection BSON value
    let projection = projection.consume_into_document();
    let projection: &Bson = match projection.get("projection") {
        Some(projection) => projection,
        None => return Err("The projection parameter does not contain a projection".to_string()),
    };

    Projection::parse_bson(projection)
}

unsafe fn get_sort(sort: wrapped_bson) -> Result<Sort, String> {
    let sort = sort.consume_into_document();
    let sort: &Bson = match sort.get("sort") {
        Some(sort) => sort,
        None => return Err("The sort parameter does not contain a sort order.".to_string()),
    };

    Sort::parse_bson(sort)
}

fn parse_compression(compression: u32) -> Result<Compression, String> {
    match compression {
        0 => Ok(Compression::None),
        1 => Ok(Compression::Zip),
        _ => Err("The compression parameter is not a valid compression type.".to_string()),
    }
}

// Helper functions
#[no_mangle]
pub unsafe extern "C" fn notmongo_test_function(value: usize) -> usize {
    value + 1
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_get_metadata() -> wrapped_bson {
    wrapped_bson::from(bson::doc! {
        "version": env!("CARGO_PKG_VERSION")
    })
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_malloc(n_bytes: usize) -> *mut ffi::c_void {
    libc::malloc(n_bytes)
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_free(value: *mut ffi::c_void) {
    libc::free(value);
}

// Database creation / destruction
#[no_mangle]
pub unsafe extern "C" fn notmongo_db_new_empty() -> *mut ffi::c_void {
    let db = libc::malloc(std::mem::size_of::<Database>()) as *mut Database;
    db.write(Database::new_empty());
    db as *mut ffi::c_void
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_free(db: *mut ffi::c_void) {
    // Get a pointer
    let db = db as *mut Database;

    // Free resources
    db.drop_in_place();

    // Deallocate the memory
    libc::free(db as *mut libc::c_void);
}

// Data Loading
#[no_mangle]
pub unsafe extern "C" fn notmongo_db_populate_from_bson_value(
    db: *mut ffi::c_void,
    contents: wrapped_bson,
) -> wrapped_bson {
    let contents = contents.consume_into_document();

    let db = db as *mut Database;
    match Database::new_from_bson_value(&contents.into()) {
        Ok(new_db) => {
            db.write(new_db);
            wrapped_bson::new_success_null()
        }
        Err(error) => wrapped_bson::new_error(&error),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_populate_from_bson_file(
    db: *mut ffi::c_void,
    file_name: *const libc::c_char,
    compression: u32,
) -> wrapped_bson {
    let file_name = match ffi::CStr::from_ptr(file_name).to_str() {
        Ok(s) => s,
        Err(_) => return wrapped_bson::new_error("The file name is not a valid UTF-8 C-String"),
    };

    let compression = match parse_compression(compression) {
        Ok(compression) => compression,
        Err(error) => return wrapped_bson::new_error(&error),
    };

    let db = db as *mut Database;
    let res = match Database::new_from_bson_file(&file_name, compression) {
        Ok(new_db) => {
            db.write(new_db);
            wrapped_bson::new_success_null()
        }
        Err(error) => wrapped_bson::new_error(&error),
    };
    res
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_populate_from_json_string(
    db: *mut ffi::c_void,
    contents: wrapped_bson,
) -> wrapped_bson {
    let args = contents.consume_into_document();
    let contents = match args.get_str("contents") {
        Ok(contents) => contents,
        Err(_) => return wrapped_bson::new_error("Missing an argument for `contents`."),
    };

    let db = db as *mut Database;
    let res = match Database::new_from_json_string(&contents) {
        Ok(new_db) => {
            db.write(new_db);
            wrapped_bson::new_success_null()
        }
        Err(error) => wrapped_bson::new_error(&error),
    };
    res
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_populate_from_json_file(
    db: *mut ffi::c_void,
    file_name: *const libc::c_char,
    compression: u32,
) -> wrapped_bson {
    let file_name = match ffi::CStr::from_ptr(file_name).to_str() {
        Ok(s) => s,
        Err(_) => return wrapped_bson::new_error("The file name is not a valid UTF-8 C-String"),
    };

    let compression = match parse_compression(compression) {
        Ok(compression) => compression,
        Err(error) => return wrapped_bson::new_error(&error),
    };

    let db = db as *mut Database;
    let res = match Database::new_from_json_file(&file_name, compression) {
        Ok(new_db) => {
            db.write(new_db);
            wrapped_bson::new_success_null()
        }
        Err(error) => wrapped_bson::new_error(&error),
    };
    res
}

// Data Dumping
#[no_mangle]
pub unsafe extern "C" fn notmongo_db_dump_to_json_file(
    db: *mut ffi::c_void,
    path: *const libc::c_char,
    relaxed: bool,
    format: bool,
    n_backups: u32,
    compression: u32,
) -> wrapped_bson {
    let db = db as *mut Database;
    let path = match ffi::CStr::from_ptr(path).to_str() {
        Ok(path) => path,
        Err(_) => return wrapped_bson::new_error("The path is not a valid UTF-8 C-String."),
    };

    let compression = match parse_compression(compression) {
        Ok(compression) => compression,
        Err(error) => return wrapped_bson::new_error(&error),
    };

    match (*db).dump_to_json_file(&path, relaxed, format, n_backups, compression) {
        Ok(_) => wrapped_bson::new_success_null(),
        Err(error) => wrapped_bson::new_error(&error),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_dump_to_json_string(
    db: *mut ffi::c_void,
    relaxed: bool,
    format: bool,
) -> wrapped_bson {
    let db = db as *mut Database;

    match (*db).dump_to_json_string(relaxed, format) {
        Ok(result) => wrapped_bson::new_success(&bson::bson!(result)),
        Err(error) => wrapped_bson::new_error(&error),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_dump_to_bson_file(
    db: *mut ffi::c_void,
    path: *const libc::c_char,
    n_backups: u32,
    compression: u32,
) -> wrapped_bson {
    let db = db as *mut Database;
    let path = match ffi::CStr::from_ptr(path).to_str() {
        Ok(path) => path,
        Err(_) => return wrapped_bson::new_error("The path is not a valid UTF-8 C-String."),
    };

    let compression = match parse_compression(compression) {
        Ok(compression) => compression,
        Err(error) => return wrapped_bson::new_error(&error),
    };

    match (*db).dump_to_bson_file(&path, n_backups, compression) {
        Ok(_) => wrapped_bson::new_success_null(),
        Err(error) => wrapped_bson::new_error(&error),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_dump_to_bson_value(db: *mut ffi::c_void) -> wrapped_bson {
    let db = db as *mut Database;

    wrapped_bson::new_success(&(*db).dump_to_bson_value().into())
}

// Query: Database Meta
#[no_mangle]
pub unsafe extern "C" fn notmongo_db_drop_collection(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
) -> wrapped_bson {
    let db = db as *mut Database;
    // Convert the string to Rust
    let collection_name = match ffi::CStr::from_ptr(collection_name).to_str() {
        Ok(s) => s,
        Err(_) => {
            return wrapped_bson::new_error("The collection name is not a valid UTF-8 C-String")
        }
    };

    // Drop the collection, if it exists
    (*db).drop_collection(collection_name);

    wrapped_bson::new_success_null()
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_n_collections(db: *mut ffi::c_void) -> wrapped_bson {
    let db = db as *mut Database;
    wrapped_bson::new_success(&bson::bson!((*db).n_collections() as i64))
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_db_collection_names(db: *mut ffi::c_void) -> wrapped_bson {
    let db = db as *mut Database;

    let names_bson = (*db)
        .collection_names()
        .into_iter()
        .map(|name| Bson::String(name))
        .collect();

    wrapped_bson::new_success(&Bson::Array(names_bson))
}

// Query: Collection Meta
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_count_all_documents(
    db: *mut ffi::c_void,
    collection: *const libc::c_char,
) -> wrapped_bson {
    let db = db as *mut Database;
    let collection = match ffi::CStr::from_ptr(collection).to_str() {
        Ok(collection) => collection,
        Err(_) => {
            return wrapped_bson::new_error("The collection name is not a valid UTF-8 C-String.")
        }
    };
    let collection = (*db).collection_mut(collection);

    wrapped_bson::new_success(&bson::bson!(collection.len() as i64))
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_count_documents(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    skip: usize,
    limit: usize,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.count_documents(&filter, skip, limit) {
        Ok(result) => wrapped_bson::new_success(&bson::bson!(result as i64)),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

// Query: Delete
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_delete_many(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.delete_many(&filter) {
        Ok(result) => wrapped_bson::new_delete_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_delete_one(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.delete_one(&filter) {
        Ok(result) => wrapped_bson::new_delete_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

// Query: Find
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_find(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    projection: wrapped_bson,
    skip: usize,
    limit: usize,
    sort: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let projection = match get_projection(projection) {
        Ok(projection) => projection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let sort = match get_sort(sort) {
        Ok(sort) => sort,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let result: Vec<Bson> = collection
        .find(&filter, &projection, skip, limit, &sort)
        .into_iter()
        .map(|doc| Bson::Document(doc))
        .collect();

    wrapped_bson::new_success(&Bson::Array(result))
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_find_one(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    projection: wrapped_bson,
    skip: usize,
    sort: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let projection = match get_projection(projection) {
        Ok(projection) => projection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let sort = match get_sort(sort) {
        Ok(sort) => sort,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.find_one(&filter, &projection, skip, &sort) {
        Some(doc) => wrapped_bson::new_success(&Bson::Document(doc)),
        None => wrapped_bson::new_success_null(),
    }
}

// Query: Insert
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_insert_one(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    document: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let document = document.consume_into_document();

    match collection.insert_one(document) {
        Ok(result) => wrapped_bson::new_insert_one_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_insert_many(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    documents: wrapped_bson,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let raw_documents: Vec<Bson> = match documents.consume_into_document().get_array("documents") {
        Ok(documents) => documents.clone(), // TODO: how to do this without cloning?
        Err(_) => return wrapped_bson::new_error("Invalid documents argument."),
    };

    let mut documents: Vec<bson::Document> = Vec::new();
    for document in raw_documents {
        match document {
            Bson::Document(document) => documents.push(document),
            _ => return wrapped_bson::new_error("The values to insert have to be documents."),
        }
    }

    match collection.insert_many(documents) {
        Ok(result) => wrapped_bson::new_insert_many_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

// Query: Replace
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_replace_one(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    replacement: wrapped_bson,
    upsert: bool,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let replacement = wrapped_bson::consume_into_document(replacement);

    match collection.replace_one(&filter, replacement, upsert) {
        Ok(result) => wrapped_bson::new_update_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

// Query: Update
#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_update_one(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    update: wrapped_bson,
    upsert: bool,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let update = match get_update(update) {
        Ok(update) => update,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.update_one(&filter, &update, upsert) {
        Ok(result) => wrapped_bson::new_update_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}

#[no_mangle]
pub unsafe extern "C" fn notmongo_collection_update_many(
    db: *mut ffi::c_void,
    collection_name: *const libc::c_char,
    filter: wrapped_bson,
    update: wrapped_bson,
    upsert: bool,
) -> wrapped_bson {
    let db = db as *mut Database;

    let collection = match get_collection(&mut *db, collection_name) {
        Ok(collection) => collection,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let filter = match get_filter(filter) {
        Ok(filter) => filter,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    let update = match get_update(update) {
        Ok(update) => update,
        Err(message) => return wrapped_bson::new_error(&message),
    };

    match collection.update_many(&filter, &update, upsert) {
        Ok(result) => wrapped_bson::new_update_result(result),
        Err(message) => wrapped_bson::new_error(&message),
    }
}