@@ -2526,7 +2526,6 @@ pet_setup (mongoc_collection_t *cats_collection,
2526
2526
return ok ;
2527
2527
}
2528
2528
2529
-
2530
2529
/*
2531
2530
* Increment 'accumulator' by the amount of adoptable pets in the given
2532
2531
* collection.
@@ -2603,17 +2602,17 @@ accumulate_adoptable_count (const mongoc_client_session_t *cs,
2603
2602
static void
2604
2603
test_example_59 (mongoc_database_t * db )
2605
2604
{
2606
- /* Start Snapshot Query Example 1 */
2607
2605
mongoc_client_t * client = NULL ;
2606
+ client = test_framework_new_default_client ();
2607
+
2608
+ /* Start Snapshot Query Example 1 */
2608
2609
mongoc_client_session_t * cs = NULL ;
2609
2610
mongoc_collection_t * cats_collection = NULL ;
2610
2611
mongoc_collection_t * dogs_collection = NULL ;
2611
2612
int64_t adoptable_pets_count = 0 ;
2612
2613
bson_error_t error ;
2613
2614
mongoc_session_opt_t * session_opts ;
2614
2615
2615
- client = test_framework_new_default_client ();
2616
-
2617
2616
cats_collection = mongoc_client_get_collection (client , "pets" , "cats" );
2618
2617
dogs_collection = mongoc_client_get_collection (client , "pets" , "dogs" );
2619
2618
@@ -2676,6 +2675,157 @@ test_example_59 (mongoc_database_t *db)
2676
2675
/* End Snapshot Query Example 1 Post */
2677
2676
}
2678
2677
2678
+ static bool
2679
+ retail_setup (mongoc_collection_t * sales_collection )
2680
+ {
2681
+ bool ok = true;
2682
+ bson_t * doc = NULL ;
2683
+ bson_error_t error ;
2684
+ struct timeval tv ;
2685
+ int64_t unix_time_now = 0 ;
2686
+
2687
+ if (bson_gettimeofday (& tv )) {
2688
+ MONGOC_ERROR ("could not get time of day" );
2689
+ goto cleanup ;
2690
+ }
2691
+ unix_time_now = 1000 * tv .tv_sec ;
2692
+
2693
+ mongoc_collection_drop (sales_collection , NULL );
2694
+
2695
+ doc = BCON_NEW ("shoeType" ,
2696
+ BCON_UTF8 ("boot" ),
2697
+ "price" ,
2698
+ BCON_INT64 (30 ),
2699
+ "saleDate" ,
2700
+ BCON_DATE_TIME (unix_time_now ));
2701
+
2702
+ ok =
2703
+ mongoc_collection_insert_one (sales_collection , doc , NULL , NULL , & error );
2704
+ if (!ok ) {
2705
+ MONGOC_ERROR ("insert into retail.sales failed: %s" , error .message );
2706
+ goto cleanup ;
2707
+ }
2708
+
2709
+ cleanup :
2710
+ bson_destroy (doc );
2711
+ return ok ;
2712
+ }
2713
+
2714
+
2715
+ static void
2716
+ test_example_60 (mongoc_database_t * db )
2717
+ {
2718
+ mongoc_client_t * client = NULL ;
2719
+ client = test_framework_new_default_client ();
2720
+
2721
+ /* Start Snapshot Query Example 2 */
2722
+ mongoc_client_session_t * cs = NULL ;
2723
+ mongoc_collection_t * sales_collection = NULL ;
2724
+ bson_error_t error ;
2725
+ mongoc_session_opt_t * session_opts ;
2726
+ bson_t * pipeline = NULL ;
2727
+ bson_t opts = BSON_INITIALIZER ;
2728
+ mongoc_cursor_t * cursor = NULL ;
2729
+ const bson_t * doc = NULL ;
2730
+ bool ok = true;
2731
+ bson_iter_t iter ;
2732
+ int64_t total_sales = 0 ;
2733
+
2734
+ sales_collection = mongoc_client_get_collection (client , "retail" , "sales" );
2735
+
2736
+ /* seed 'retail.sales' with example data */
2737
+ if (!retail_setup (sales_collection )) {
2738
+ goto cleanup ;
2739
+ }
2740
+
2741
+ /* start a snapshot session */
2742
+ session_opts = mongoc_session_opts_new ();
2743
+ mongoc_session_opts_set_snapshot (session_opts , true);
2744
+ cs = mongoc_client_start_session (client , session_opts , & error );
2745
+ mongoc_session_opts_destroy (session_opts );
2746
+ if (!cs ) {
2747
+ MONGOC_ERROR ("Could not start session: %s" , error .message );
2748
+ goto cleanup ;
2749
+ }
2750
+
2751
+ if (!mongoc_client_session_append (cs , & opts , & error )) {
2752
+ MONGOC_ERROR ("could not apply session options: %s" , error .message );
2753
+ goto cleanup ;
2754
+ }
2755
+
2756
+ pipeline = BCON_NEW ("pipeline" ,
2757
+ "[" ,
2758
+ "{" ,
2759
+ "$match" ,
2760
+ "{" ,
2761
+ "$expr" ,
2762
+ "{" ,
2763
+ "$gt" ,
2764
+ "[" ,
2765
+ "$saleDate" ,
2766
+ "{" ,
2767
+ "$dateSubtract" ,
2768
+ "{" ,
2769
+ "startDate" ,
2770
+ "$$NOW" ,
2771
+ "unit" ,
2772
+ BCON_UTF8 ("day" ),
2773
+ "amount" ,
2774
+ BCON_INT64 (1 ),
2775
+ "}" ,
2776
+ "}" ,
2777
+ "]" ,
2778
+ "}" ,
2779
+ "}" ,
2780
+ "}" ,
2781
+ "{" ,
2782
+ "$count" ,
2783
+ BCON_UTF8 ("totalDailySales" ),
2784
+ "}" ,
2785
+ "]" );
2786
+
2787
+ cursor = mongoc_collection_aggregate (
2788
+ sales_collection , MONGOC_QUERY_NONE , pipeline , & opts , NULL );
2789
+ bson_destroy (& opts );
2790
+
2791
+ ok = mongoc_cursor_next (cursor , & doc );
2792
+
2793
+ if (mongoc_cursor_error (cursor , & error )) {
2794
+ MONGOC_ERROR ("could not get totalDailySales: %s" , error .message );
2795
+ ok = false;
2796
+ goto cleanup ;
2797
+ }
2798
+
2799
+ if (!ok ) {
2800
+ MONGOC_ERROR ("%s" , "cursor has no results" );
2801
+ goto cleanup ;
2802
+ }
2803
+
2804
+ ok = bson_iter_init_find (& iter , doc , "totalDailySales" );
2805
+ if (ok ) {
2806
+ total_sales = bson_iter_as_int64 (& iter );
2807
+ } else {
2808
+ MONGOC_ERROR ("%s" , "missing key: 'totalDailySales'" );
2809
+ goto cleanup ;
2810
+ }
2811
+
2812
+ /* End Snapshot Query Example 2 */
2813
+
2814
+ if (total_sales != 1 ) {
2815
+ MONGOC_ERROR ("there should be exactly 1 total_sales, found: %" PRId64 ,
2816
+ total_sales );
2817
+ }
2818
+
2819
+ /* Start Snapshot Query Example 2 Post */
2820
+ cleanup :
2821
+ mongoc_collection_destroy (sales_collection );
2822
+ mongoc_client_session_destroy (cs );
2823
+ mongoc_cursor_destroy (cursor );
2824
+ bson_destroy (pipeline );
2825
+ mongoc_client_destroy (client );
2826
+ /* End Snapshot Query Example 2 Post */
2827
+ }
2828
+
2679
2829
/* clang-format off */
2680
2830
2681
2831
typedef struct {
@@ -4205,6 +4355,7 @@ test_sample_commands (void)
4205
4355
test_sample_command (test_example_58 , 58 , db , collection , false);
4206
4356
test_sample_command (test_example_56 , 56 , db , collection , true);
4207
4357
test_sample_command (test_example_59 , 59 , db , collection , true);
4358
+ test_sample_command (test_example_60 , 60 , db , collection , true);
4208
4359
test_sample_change_stream_command (test_example_change_stream , db );
4209
4360
test_sample_causal_consistency (client );
4210
4361
test_sample_aggregation (db );
0 commit comments