@@ -701,6 +701,158 @@ test_upsert_unordered (void)
701
701
}
702
702
703
703
704
+ static void
705
+ test_upserted_index (bool ordered )
706
+ {
707
+ mongoc_bulk_operation_t * bulk ;
708
+ mongoc_collection_t * collection ;
709
+ mongoc_client_t * client ;
710
+ bool has_write_cmds ;
711
+
712
+ bson_error_t error ;
713
+ bson_t reply ;
714
+ bson_t * emp = tmp_bson ("{}" );
715
+ bson_t * inc = tmp_bson ("{'$inc': {'b': 1}}" );
716
+ bool r ;
717
+
718
+ client = test_framework_client_new (NULL );
719
+ assert (client );
720
+ has_write_cmds = server_has_write_commands (client );
721
+
722
+ collection = get_test_collection (client , "test_upserted_index" );
723
+ assert (collection );
724
+
725
+ bulk = mongoc_collection_create_bulk_operation (collection , ordered , NULL );
726
+ assert (bulk );
727
+
728
+ mongoc_bulk_operation_insert (bulk , emp );
729
+ mongoc_bulk_operation_insert (bulk , emp );
730
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'i': 2}" ));
731
+ mongoc_bulk_operation_update (bulk ,
732
+ tmp_bson ("{'i': 3}" ),
733
+ inc , false);
734
+ /* upsert */
735
+ mongoc_bulk_operation_update (bulk ,
736
+ tmp_bson ("{'i': 4}" ),
737
+ inc , true);
738
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'i': 5}" ));
739
+ mongoc_bulk_operation_remove_one (bulk , tmp_bson ("{'i': 6}" ));
740
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 7}" ), emp , false);
741
+ /* upsert */
742
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 8}" ), emp , true);
743
+ /* upsert */
744
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 9}" ), emp , true);
745
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'i': 10}" ));
746
+ mongoc_bulk_operation_insert (bulk , emp );
747
+ mongoc_bulk_operation_insert (bulk , emp );
748
+ mongoc_bulk_operation_update (bulk ,
749
+ tmp_bson ("{'i': 13}" ),
750
+ inc , false);
751
+ /* upsert */
752
+ mongoc_bulk_operation_update (bulk ,
753
+ tmp_bson ("{'i': 14}" ),
754
+ inc , true);
755
+ mongoc_bulk_operation_insert (bulk , emp );
756
+ /* upserts */
757
+ mongoc_bulk_operation_update (bulk ,
758
+ tmp_bson ("{'i': 16}" ),
759
+ inc , true);
760
+ mongoc_bulk_operation_update (bulk ,
761
+ tmp_bson ("{'i': 17}" ),
762
+ inc , true);
763
+ /* non-upsert */
764
+ mongoc_bulk_operation_update (bulk ,
765
+ tmp_bson ("{'i': 18}" ),
766
+ inc , false);
767
+ /* upserts */
768
+ mongoc_bulk_operation_update (bulk ,
769
+ tmp_bson ("{'i': 19}" ),
770
+ inc , true);
771
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 20}" ), emp , true);
772
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 21}" ), emp , true);
773
+ mongoc_bulk_operation_replace_one (bulk , tmp_bson ("{'i': 22}" ), emp , true);
774
+ mongoc_bulk_operation_update (bulk ,
775
+ tmp_bson ("{'i': 23}" ),
776
+ inc , true);
777
+ /* non-upsert */
778
+ mongoc_bulk_operation_update_one (bulk ,
779
+ tmp_bson ("{'i': 24}" ),
780
+ inc , false);
781
+ /* upsert */
782
+ mongoc_bulk_operation_update_one (bulk ,
783
+ tmp_bson ("{'i': 25}" ),
784
+ inc , true);
785
+ /* non-upserts */
786
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'i': 26}" ));
787
+ mongoc_bulk_operation_remove (bulk , tmp_bson ("{'i': 27}" ));
788
+ mongoc_bulk_operation_update_one (bulk ,
789
+ tmp_bson ("{'i': 28}" ),
790
+ inc , false);
791
+ mongoc_bulk_operation_update_one (bulk ,
792
+ tmp_bson ("{'i': 29}" ),
793
+ inc , false);
794
+ /* each update modifies existing 16 docs, but only increments index by one */
795
+ mongoc_bulk_operation_update (bulk , emp , inc , false);
796
+ mongoc_bulk_operation_update (bulk , emp , inc , false);
797
+ /* upsert */
798
+ mongoc_bulk_operation_update_one (bulk ,
799
+ tmp_bson ("{'i': 32}" ),
800
+ inc , true);
801
+
802
+
803
+
804
+ r = (bool )mongoc_bulk_operation_execute (bulk , & reply , & error );
805
+ if (!r ) {
806
+ fprintf (stderr , "bulk failed: %s\n" , error .message );
807
+ abort ();
808
+ }
809
+
810
+ ASSERT_MATCH (& reply , "{'nInserted': 5,"
811
+ " 'nRemoved': 0,"
812
+ " 'nMatched': 34,"
813
+ " 'nUpserted': 13,"
814
+ " 'upserted.0.index': 4,"
815
+ " 'upserted.1.index': 8,"
816
+ " 'upserted.2.index': 9,"
817
+ " 'upserted.3.index': 14,"
818
+ " 'upserted.4.index': 16,"
819
+ " 'upserted.5.index': 17,"
820
+ " 'upserted.6.index': 19,"
821
+ " 'upserted.7.index': 20,"
822
+ " 'upserted.8.index': 21,"
823
+ " 'upserted.9.index': 22,"
824
+ " 'upserted.10.index': 23,"
825
+ " 'upserted.11.index': 25,"
826
+ " 'upserted.12.index': 32,"
827
+ " 'writeErrors': []}" );
828
+
829
+ check_n_modified (has_write_cmds , & reply , 34 );
830
+ assert_count (18 , collection );
831
+
832
+ r = mongoc_collection_drop (collection , & error );
833
+ assert (r );
834
+
835
+ bson_destroy (& reply );
836
+ mongoc_bulk_operation_destroy (bulk );
837
+ mongoc_collection_destroy (collection );
838
+ mongoc_client_destroy (client );
839
+ }
840
+
841
+
842
+ static void
843
+ test_upserted_index_ordered (void )
844
+ {
845
+ test_upserted_index (true);
846
+ }
847
+
848
+
849
+ static void
850
+ test_upserted_index_unordered (void )
851
+ {
852
+ test_upserted_index (false);
853
+ }
854
+
855
+
704
856
static void
705
857
test_update_one (bool ordered )
706
858
{
0 commit comments