@@ -2636,6 +2636,350 @@ test_example_change_stream (mongoc_database_t *db)
2636
2636
}
2637
2637
2638
2638
2639
+ static void
2640
+ test_sample_aggregation (mongoc_database_t * db )
2641
+ {
2642
+ /* Start Aggregation Example 1 */
2643
+ mongoc_collection_t * collection ;
2644
+ bson_t * pipeline ;
2645
+ mongoc_cursor_t * cursor ;
2646
+ bson_error_t error ;
2647
+ const bson_t * doc ;
2648
+
2649
+ collection = mongoc_database_get_collection (db , "sales" );
2650
+
2651
+ pipeline = BCON_NEW ("pipeline" , "[" ,
2652
+ "{" ,
2653
+ "$match" , "{" ,
2654
+ "items.fruit" , BCON_UTF8 ("banana" ),
2655
+ "}" ,
2656
+ "}" ,
2657
+ "{" ,
2658
+ "$sort" , "{" ,
2659
+ "date" , BCON_INT32 (1 ),
2660
+ "}" ,
2661
+ "}" ,
2662
+ "]" );
2663
+
2664
+ cursor = mongoc_collection_aggregate (
2665
+ collection , MONGOC_QUERY_NONE , pipeline , NULL , NULL );
2666
+ bson_destroy (pipeline );
2667
+
2668
+ while (mongoc_cursor_next (cursor , & doc )) {
2669
+ /* Do something with each doc here */
2670
+ }
2671
+
2672
+ if (mongoc_cursor_error (cursor , & error )) {
2673
+ MONGOC_ERROR ("%s\n" , error .message );
2674
+ }
2675
+
2676
+ mongoc_cursor_destroy (cursor );
2677
+ /* End Aggregation Example 1 */
2678
+
2679
+ /* Start Aggregation Example 2 */
2680
+ pipeline = BCON_NEW ("pipeline" , "[" ,
2681
+ "{" ,
2682
+ "$unwind" , BCON_UTF8 ("$items" ),
2683
+ "}" ,
2684
+ "{" ,
2685
+ "$match" , "{" ,
2686
+ "items.fruit" , BCON_UTF8 ("banana" ),
2687
+ "}" ,
2688
+ "}" ,
2689
+ "{" ,
2690
+ "$group" , "{" ,
2691
+ "_id" , "{" ,
2692
+ "day" , "{" ,
2693
+ "$dayOfWeek" , BCON_UTF8 ("$date" ),
2694
+ "}" ,
2695
+ "}" ,
2696
+ "count" , "{" ,
2697
+ "$sum" , BCON_UTF8 ("$items.quantity" ),
2698
+ "}" ,
2699
+ "}" ,
2700
+ "}" ,
2701
+ "{" ,
2702
+ "$project" , "{" ,
2703
+ "dayOfWeek" , BCON_UTF8 ("$_id.day" ),
2704
+ "numberSold" , BCON_UTF8 ("$count" ),
2705
+ "_id" , BCON_INT32 (0 ),
2706
+ "}" ,
2707
+ "}" ,
2708
+ "{" ,
2709
+ "$sort" , "{" ,
2710
+ "numberSold" , BCON_INT32 (1 ),
2711
+ "}" ,
2712
+ "}" ,
2713
+ "]" );
2714
+
2715
+ cursor = mongoc_collection_aggregate (
2716
+ collection , MONGOC_QUERY_NONE , pipeline , NULL , NULL );
2717
+ bson_destroy (pipeline );
2718
+
2719
+ while (mongoc_cursor_next (cursor , & doc )) {
2720
+ /* Do something with each doc here */
2721
+ }
2722
+
2723
+ if (mongoc_cursor_error (cursor , & error )) {
2724
+ MONGOC_ERROR ("%s\n" , error .message );
2725
+ }
2726
+
2727
+ mongoc_cursor_destroy (cursor );
2728
+ /* End Aggregation Example 2 */
2729
+
2730
+ /* Start Aggregation Example 3 */
2731
+ pipeline = BCON_NEW ("pipeline" , "[" ,
2732
+ "{" ,
2733
+ "$unwind" , BCON_UTF8 ("$items" ),
2734
+ "}" ,
2735
+ "{" ,
2736
+ "$group" , "{" ,
2737
+ "_id" , "{" ,
2738
+ "day" , "{" ,
2739
+ "$dayOfWeek" , BCON_UTF8 ("$date" ),
2740
+ "}" ,
2741
+ "}" ,
2742
+ "items_sold" , "{" ,
2743
+ "$sum" , BCON_UTF8 ("$items.quantity" ),
2744
+ "}" ,
2745
+ "revenue" , "{" ,
2746
+ "$sum" , "{" ,
2747
+ "$multiply" , "[" ,
2748
+ BCON_UTF8 ("$items.quantity" ),
2749
+ BCON_UTF8 ("$items.price" ),
2750
+ "]" ,
2751
+ "}" ,
2752
+ "}" ,
2753
+ "}" ,
2754
+ "}" ,
2755
+ "{" ,
2756
+ "$project" , "{" ,
2757
+ "day" , BCON_UTF8 ("$_id.day" ),
2758
+ "revenue" , BCON_INT32 (1 ),
2759
+ "items_sold" , BCON_INT32 (1 ),
2760
+ "discount" , "{" ,
2761
+ "$cond" , "{" ,
2762
+ "if" , "{" ,
2763
+ "$lte" , "[" ,
2764
+ "$revenue" ,
2765
+ BCON_INT32 (250 ),
2766
+ "]" ,
2767
+ "}" ,
2768
+ "then" , BCON_INT32 (25 ),
2769
+ "else" , BCON_INT32 (0 ),
2770
+ "}" ,
2771
+ "}" ,
2772
+ "}" ,
2773
+ "}" ,
2774
+ "]" );
2775
+
2776
+ cursor = mongoc_collection_aggregate (
2777
+ collection , MONGOC_QUERY_NONE , pipeline , NULL , NULL );
2778
+ bson_destroy (pipeline );
2779
+
2780
+ while (mongoc_cursor_next (cursor , & doc )) {
2781
+ /* Do something with each doc here */
2782
+ }
2783
+
2784
+ if (mongoc_cursor_error (cursor , & error )) {
2785
+ MONGOC_ERROR ("%s\n" , error .message );
2786
+ }
2787
+
2788
+ mongoc_cursor_destroy (cursor );
2789
+ /* End Aggregation Example 3 */
2790
+
2791
+ mongoc_collection_destroy (collection );
2792
+
2793
+
2794
+ /* Need MongoDB 3.6 to use unrelated subqueries */
2795
+ if (test_framework_skip_if_max_wire_version_less_than_6 ()){
2796
+
2797
+ /* Start Aggregation Example 4 */
2798
+ collection = mongoc_database_get_collection (db , "air_alliances" );
2799
+ pipeline = BCON_NEW ("pipeline" , "[" ,
2800
+ "{" ,
2801
+ "$lookup" , "{" ,
2802
+ "from" , BCON_UTF8 ("air_airlines" ),
2803
+ "let" , "{" ,
2804
+ "constituents" , BCON_UTF8 ("$airlines" ),
2805
+ "}" ,
2806
+ "pipeline" , "[" ,
2807
+ "{" ,
2808
+ "$match" , "{" ,
2809
+ "$expr" , "{" ,
2810
+ "$in" , "[" ,
2811
+ "$name" ,
2812
+ BCON_UTF8 ("$$constituents" ),
2813
+ "]" ,
2814
+ "}" ,
2815
+ "}" ,
2816
+ "}" ,
2817
+ "]" ,
2818
+ "as" , BCON_UTF8 ("airlines" ),
2819
+ "}" ,
2820
+ "}" ,
2821
+ "{" ,
2822
+ "$project" , "{" ,
2823
+ "_id" , BCON_INT32 (0 ),
2824
+ "name" , BCON_INT32 (1 ),
2825
+ "airlines" , "{" ,
2826
+ "$filter" , "{" ,
2827
+ "input" , BCON_UTF8 ("$airlines" ),
2828
+ "as" , BCON_UTF8 ("airline" ),
2829
+ "cond" , "{" ,
2830
+ "$eq" , "[" ,
2831
+ BCON_UTF8 ("$$airline.country" ),
2832
+ BCON_UTF8 ("Canada" ),
2833
+ "]" ,
2834
+ "}" ,
2835
+ "}" ,
2836
+ "}" ,
2837
+ "}" ,
2838
+ "}" ,
2839
+ "]" );
2840
+
2841
+ cursor = mongoc_collection_aggregate (
2842
+ collection , MONGOC_QUERY_NONE , pipeline , NULL , NULL );
2843
+ bson_destroy (pipeline );
2844
+
2845
+ while (mongoc_cursor_next (cursor , & doc )) {
2846
+ /* Do something with each doc here */
2847
+ }
2848
+
2849
+ if (mongoc_cursor_error (cursor , & error )) {
2850
+ MONGOC_ERROR ("%s\n" , error .message );
2851
+ }
2852
+
2853
+ mongoc_cursor_destroy (cursor );
2854
+ mongoc_collection_destroy (collection );
2855
+ /* End Aggregation Example 4 */
2856
+
2857
+ }
2858
+
2859
+ ASSERT_NO_CAPTURED_LOGS ("sample aggregation examples" );
2860
+ }
2861
+
2862
+ static void
2863
+ test_sample_run_command (mongoc_database_t * db )
2864
+ {
2865
+ /* Start runCommand Example 1 */
2866
+ bson_t * run_command ;
2867
+ bson_t reply ;
2868
+ bson_error_t error ;
2869
+ bool r ;
2870
+
2871
+ run_command = BCON_NEW ("buildInfo" , BCON_INT32 (1 ));
2872
+
2873
+ r = mongoc_database_write_command_with_opts (
2874
+ db , run_command , NULL /* opts */ , & reply , & error );
2875
+ bson_destroy (run_command );
2876
+
2877
+ if (!r ) {
2878
+ MONGOC_ERROR ("%s\n" , error .message );
2879
+ }
2880
+
2881
+ /* Do something with reply here */
2882
+
2883
+ bson_destroy (& reply );
2884
+ /* End runCommand Example 1 */
2885
+
2886
+ /* Start runCommand Example 2 */
2887
+ run_command = BCON_NEW ("collStats" , BCON_UTF8 ("restaurants" ));
2888
+
2889
+ r = mongoc_database_write_command_with_opts (
2890
+ db , run_command , NULL /* opts */ , & reply , & error );
2891
+ bson_destroy (run_command );
2892
+
2893
+ if (!r ) {
2894
+ MONGOC_ERROR ("%s\n" , error .message );
2895
+ }
2896
+
2897
+ /* Do something with reply here */
2898
+
2899
+ bson_destroy (& reply );
2900
+ /* End runCommand Example 2 */
2901
+
2902
+ ASSERT_NO_CAPTURED_LOGS ("sample runCommand examples" );
2903
+ }
2904
+
2905
+ static void
2906
+ test_sample_indexes (mongoc_database_t * db )
2907
+ {
2908
+ /* Start Index Example 1 */
2909
+ const char * collection_name = "records" ;
2910
+ char * index_name ;
2911
+ bson_t * create_indexes ;
2912
+ bson_t reply ;
2913
+ bson_t keys ;
2914
+ bson_error_t error ;
2915
+ bool r ;
2916
+
2917
+ bson_init (& keys );
2918
+ BSON_APPEND_INT32 (& keys , "score" , 1 );
2919
+ index_name = mongoc_collection_keys_to_index_string (& keys );
2920
+
2921
+ create_indexes = BCON_NEW ("createIndexes" , BCON_UTF8 (collection_name ),
2922
+ "indexes" , "[" ,
2923
+ "{" ,
2924
+ "key" , BCON_DOCUMENT (& keys ),
2925
+ "name" , BCON_UTF8 (index_name ),
2926
+ "}" ,
2927
+ "]" );
2928
+
2929
+ r = mongoc_database_write_command_with_opts (
2930
+ db , create_indexes , NULL /* opts */ , & reply , & error );
2931
+ bson_destroy (create_indexes );
2932
+ bson_free (index_name );
2933
+
2934
+ if (!r ) {
2935
+ MONGOC_ERROR ("%s\n" , error .message );
2936
+ }
2937
+
2938
+ /* Do something with reply here */
2939
+
2940
+ bson_destroy (& reply );
2941
+ bson_destroy (& keys );
2942
+ /* End Index Example 1 */
2943
+
2944
+ /* Start Index Example 2 */
2945
+ collection_name = "restaurants" ;
2946
+
2947
+ bson_init (& keys );
2948
+ BSON_APPEND_INT32 (& keys , "cuisine" , 1 );
2949
+ BSON_APPEND_INT32 (& keys , "name" , 1 );
2950
+ index_name = mongoc_collection_keys_to_index_string (& keys );
2951
+ create_indexes = BCON_NEW ("createIndexes" , BCON_UTF8 (collection_name ),
2952
+ "indexes" , "[" ,
2953
+ "{" ,
2954
+ "key" , BCON_DOCUMENT (& keys ),
2955
+ "partialFilterExpression" , "{" ,
2956
+ "rating" , "{" ,
2957
+ "$gt" , BCON_INT32 (5 ),
2958
+ "}" ,
2959
+ "}" ,
2960
+ "name" , BCON_UTF8 (index_name ),
2961
+ "}" ,
2962
+ "]" );
2963
+
2964
+ r = mongoc_database_write_command_with_opts (
2965
+ db , create_indexes , NULL /* opts */ , & reply , & error );
2966
+ bson_destroy (create_indexes );
2967
+ bson_free (index_name );
2968
+
2969
+ if (!r ) {
2970
+ MONGOC_ERROR ("%s\n" , error .message );
2971
+ }
2972
+
2973
+ /* Do something with reply here */
2974
+
2975
+ bson_destroy (& reply );
2976
+ bson_destroy (& keys );
2977
+ /* End Index Example 2 */
2978
+
2979
+ ASSERT_NO_CAPTURED_LOGS ("sample index examples" );
2980
+ }
2981
+
2982
+
2639
2983
static void
2640
2984
test_sample_commands (void )
2641
2985
{
@@ -2704,6 +3048,9 @@ test_sample_commands (void)
2704
3048
test_sample_command (test_example_58 , 58 , db , collection , false);
2705
3049
test_sample_command (test_example_56 , 56 , db , collection , true);
2706
3050
test_sample_change_stream_command (test_example_change_stream , db );
3051
+ test_sample_aggregation (db );
3052
+ test_sample_indexes (db );
3053
+ test_sample_run_command (db );
2707
3054
2708
3055
mongoc_collection_drop (collection , NULL );
2709
3056
0 commit comments