@@ -400,6 +400,8 @@ private:
400
400
// / Mixin for functions for interacting with Mongo collections.
401
401
mixin template MongoSchema()
402
402
{
403
+ import vibe.db.mongo.collection;
404
+
403
405
import std.typecons : Nullable;
404
406
import std.range : isInputRange, ElementType;
405
407
@@ -428,14 +430,16 @@ mixin template MongoSchema()
428
430
{
429
431
if (_schema_object_id_.valid)
430
432
{
431
- collection.update(Bson([" _id" : Bson(_schema_object_id_)]),
432
- this .toSchemaBson(), UpdateFlags.upsert);
433
+ UpdateOptions options;
434
+ options.upsert = true ;
435
+ collection.replaceOne(Bson([" _id" : Bson(_schema_object_id_)]),
436
+ this .toSchemaBson(), options);
433
437
}
434
438
else
435
439
{
436
440
_schema_object_id_ = BsonObjectID.generate;
437
441
auto bson = this .toSchemaBson();
438
- collection.insert (bson);
442
+ collection.insertOne (bson);
439
443
}
440
444
}
441
445
@@ -444,14 +448,16 @@ mixin template MongoSchema()
444
448
{
445
449
if (_schema_object_id_.valid)
446
450
{
447
- collection.update(Bson([" _id" : Bson(_schema_object_id_)]),
448
- Bson([" $set" : this .toSchemaBson()]), UpdateFlags.upsert);
451
+ UpdateOptions options;
452
+ options.upsert = true ;
453
+ collection.updateOne(Bson([" _id" : Bson(_schema_object_id_)]),
454
+ Bson([" $set" : this .toSchemaBson()]), options);
449
455
}
450
456
else
451
457
{
452
458
_schema_object_id_ = BsonObjectID.generate;
453
459
auto bson = this .toSchemaBson();
454
- collection.insert (bson);
460
+ collection.insertOne (bson);
455
461
}
456
462
}
457
463
@@ -460,7 +466,7 @@ mixin template MongoSchema()
460
466
{
461
467
if (! _schema_object_id_.valid)
462
468
return false ;
463
- collection.remove (Bson([" _id" : Bson(_schema_object_id_)]), DeleteFlags.SingleRemove );
469
+ collection.deleteOne (Bson([" _id" : Bson(_schema_object_id_)]));
464
470
return true ;
465
471
}
466
472
@@ -568,37 +574,32 @@ mixin template MongoSchema()
568
574
}
569
575
570
576
// / Finds one or more elements using a query.
571
- static typeof (this )[] find (Query! (typeof (this )) query,
572
- QueryFlags flags = QueryFlags.None, int num_skip = 0 , int num_docs_per_chunk = 0 )
577
+ static typeof (this )[] find (Query! (typeof (this )) query, FindOptions options = FindOptions.init)
573
578
{
574
- return find (query._query, flags, num_skip, num_docs_per_chunk );
579
+ return find (query._query, options );
575
580
}
576
581
577
582
// / ditto
578
- static typeof (this )[] find (T)(T query, QueryFlags flags = QueryFlags.None,
579
- int num_skip = 0 , int num_docs_per_chunk = 0 )
583
+ static typeof (this )[] find (T)(T query, FindOptions options = FindOptions.init)
580
584
{
581
585
typeof (this )[] values ;
582
- foreach (entry; collection.find(query, null , flags, num_skip, num_docs_per_chunk ))
586
+ foreach (entry; collection.find(query, options ))
583
587
{
584
588
values ~= fromSchemaBson! (typeof (this ))(entry);
585
589
}
586
590
return values ;
587
591
}
588
592
589
593
// / Finds one or more elements using a query as range.
590
- static DocumentRange! (typeof (this )) findRange (Query! (typeof (this )) query,
591
- QueryFlags flags = QueryFlags.None, int num_skip = 0 , int num_docs_per_chunk = 0 )
594
+ static DocumentRange! (typeof (this )) findRange (Query! (typeof (this )) query, FindOptions options = FindOptions.init)
592
595
{
593
- return findRange (query._query, flags, num_skip, num_docs_per_chunk );
596
+ return findRange (query._query, options );
594
597
}
595
598
596
599
// / ditto
597
- static DocumentRange! (typeof (this )) findRange (T)(T query,
598
- QueryFlags flags = QueryFlags.None, int num_skip = 0 , int num_docs_per_chunk = 0 )
600
+ static DocumentRange! (typeof (this )) findRange (T)(T query, FindOptions options = FindOptions.init)
599
601
{
600
- return DocumentRange! (typeof (this ))(collection.find(serializeToBson(query),
601
- null , flags, num_skip, num_docs_per_chunk));
602
+ return DocumentRange! (typeof (this ))(collection.find(serializeToBson(query), options));
602
603
}
603
604
604
605
// / Queries all elements from the collection as range.
@@ -616,54 +617,66 @@ mixin template MongoSchema()
616
617
617
618
if (documents.empty)
618
619
return ;
619
- collection.insert (documents.map! ((a) {
620
+ collection.insertMany (documents.map! ((a) {
620
621
a.bsonID = BsonObjectID.init;
621
622
return a.toSchemaBson;
622
623
}).array, options); // .array needed because of vibe-d issue #2185
623
624
}
624
625
625
- // / Updates a document.
626
- static void update (U)(Query! (typeof (this )) query, U update, UpdateFlags options = UpdateFlags
627
- .none)
626
+ // / Updates a document with `$dollarOperations`.
627
+ static void updateOne (U)(Query! (typeof (this )) query, U update, UpdateOptions options = UpdateOptions.init)
628
628
{
629
- update (query._query, update, options);
629
+ updateOne (query._query, update, options);
630
630
}
631
631
632
632
// / ditto
633
- static void update (T, U)(T query, U update, UpdateFlags options = UpdateFlags.none )
633
+ static void updateOne (T, U)(T query, U update, UpdateOptions options = UpdateOptions.init )
634
634
{
635
- collection.update (query, update, options);
635
+ collection.updateOne (query, update, options);
636
636
}
637
637
638
- // / Updates a document or inserts it when not existent. Shorthand for `update(..., UpdateFlags.upsert)`
639
- static void upsert (U)(Query! (typeof (this )) query, U upsert,
640
- UpdateFlags options = UpdateFlags.upsert)
638
+ // / Updates any amount of documents.
639
+ static void updateMany (U)(Query! (typeof (this )) query, U update, UpdateOptions options = UpdateOptions.init)
640
+ {
641
+ updateMany(query._query, update, options);
642
+ }
643
+
644
+ // / ditto
645
+ static void updateMany (T, U)(T query, U update, UpdateOptions options = UpdateOptions.init)
641
646
{
647
+ collection.updateMany(query, update, options);
648
+ }
649
+
650
+ // / Updates a document or inserts it when not existent. Calls `replaceOne` with `options.upsert` set to true.
651
+ static void upsert (U)(Query! (typeof (this )) query, U upsert, UpdateOptions options = UpdateOptions.init)
652
+ {
653
+ options.upsert = true ;
642
654
upsert(query._query, upsert, options);
643
655
}
644
656
645
657
// / ditto
646
- static void upsert (T, U)(T query, U update, UpdateFlags options = UpdateFlags.upsert )
658
+ static void upsert (T, U)(T query, U update, UpdateOptions options = UpdateOptions.init )
647
659
{
648
- collection.update(query, update, options);
660
+ options.upsert = true ;
661
+ collection.replaceOne(query, update, options);
649
662
}
650
663
651
- // / Deletes one or any amount of documents matching the selector based on the flags .
652
- static void remove (Query! (typeof (this )) query, DeleteFlags flags = DeleteFlags.none )
664
+ // / Deletes one or any amount of documents matching the selector based on the options .
665
+ static void remove (Query! (typeof (this )) query, DeleteOptions options = DeleteOptions.init )
653
666
{
654
- remove(query._query, flags );
667
+ remove(query._query, options );
655
668
}
656
669
657
670
// / ditto
658
- static void remove (T)(T selector, DeleteFlags flags = DeleteFlags.none )
671
+ static void remove (T)(T selector, DeleteOptions options = DeleteOptions.init )
659
672
{
660
- collection.remove (selector, flags );
673
+ collection.deleteMany (selector, options );
661
674
}
662
675
663
676
// / Removes all documents from this collection.
664
- static void removeAll ()
677
+ static void removeAll (DeleteOptions options = DeleteOptions.init )
665
678
{
666
- collection.remove( );
679
+ collection.deleteAll(options );
667
680
}
668
681
669
682
// / Drops the entire collection and all indices in the database.
@@ -681,15 +694,15 @@ mixin template MongoSchema()
681
694
// / ditto
682
695
static auto count (T)(T query)
683
696
{
684
- return collection.count (query);
697
+ return collection.countDocuments (query);
685
698
}
686
699
687
700
// / Returns the count of documents in this collection.
688
701
static auto countAll ()
689
702
{
690
703
import vibe.data.bson : Bson;
691
704
692
- return collection.count (Bson.emptyObject);
705
+ return collection.countDocuments (Bson.emptyObject);
693
706
}
694
707
695
708
// / Start of an aggregation call. Returns a pipeline with typesafe functions for modifying the pipeline and running it at the end.
@@ -754,57 +767,30 @@ void register(T)(MongoCollection collection) @safe
754
767
}
755
768
756
769
757
- static if (is (IndexOptions))
770
+ IndexOptions indexOptions;
771
+ static if (hasUDA! (member, mongoBackground))
772
+ {
773
+ indexOptions.background = true ;
774
+ }
775
+ static if (hasUDA! (member, mongoDropDuplicates))
776
+ {
777
+ indexOptions.dropDups = true ;
778
+ }
779
+ static if (hasUDA! (member, mongoSparse))
758
780
{
759
- IndexOptions indexOptions;
760
- static if (hasUDA! (member, mongoBackground))
761
- {
762
- indexOptions.background = true ;
763
- }
764
- static if (hasUDA! (member, mongoDropDuplicates))
765
- {
766
- indexOptions.dropDups = true ;
767
- }
768
- static if (hasUDA! (member, mongoSparse))
769
- {
770
- indexOptions.sparse = true ;
771
- }
772
- static if (hasUDA! (member, mongoUnique))
773
- {
774
- indexOptions.unique = true ;
775
- }
776
- static if (hasUDA! (member, mongoExpire))
777
- {
778
- indexOptions.expireAfterSeconds = cast (int )expires;
779
- }
781
+ indexOptions.sparse = true ;
780
782
}
781
- else
783
+ static if (hasUDA ! (member, mongoUnique))
782
784
{
783
- IndexFlags flags = IndexFlags.None;
784
- static if (hasUDA! (member, mongoBackground))
785
- {
786
- flags |= IndexFlags.Background;
787
- }
788
- static if (hasUDA! (member, mongoDropDuplicates))
789
- {
790
- flags |= IndexFlags.DropDuplicates;
791
- }
792
- static if (hasUDA! (member, mongoSparse))
793
- {
794
- flags |= IndexFlags.Sparse;
795
- }
796
- static if (hasUDA! (member, mongoUnique))
797
- {
798
- flags |= IndexFlags.Unique;
799
- }
800
- static if (hasUDA! (member, mongoExpire))
801
- {
802
- flags |= IndexFlags.ExpireAfterSeconds;
803
- }
804
-
805
- if (flags != IndexFlags.None || force)
806
- collection.ensureIndex([tuple(name, 1 )], flags, dur! " seconds" (expires));
785
+ indexOptions.unique = true ;
807
786
}
787
+ static if (hasUDA! (member, mongoExpire))
788
+ {
789
+ indexOptions.expireAfterSeconds = cast (int )expires;
790
+ }
791
+
792
+ if (indexOptions != indexOptions.init)
793
+ collection.createIndex([name: 1 ], indexOptions);
808
794
}
809
795
}
810
796
}
@@ -1021,7 +1007,7 @@ unittest
1021
1007
auto client = connectMongoDB(" 127.0.0.1" );
1022
1008
auto database = client.getDatabase(" test" );
1023
1009
MongoCollection users = database[" users" ];
1024
- users.remove (); // Clears collection
1010
+ users.deleteAll (); // Clears collection
1025
1011
1026
1012
struct User
1027
1013
{
@@ -1056,10 +1042,10 @@ unittest
1056
1042
User faker;
1057
1043
faker.username = " Example" ;
1058
1044
faker.hash = sha512Of(" PASSWORD" ).dup ;
1059
- faker.profilePicture = " example -avatar.png" ;
1045
+ faker.profilePicture = " faker -avatar.png" ;
1060
1046
1061
- assertThrown(faker.save());
1062
1047
// Unique username
1048
+ assertThrown(faker.save());
1063
1049
1064
1050
faker.username = " Example_" ;
1065
1051
assertNotThrown(faker.save());
@@ -1129,7 +1115,7 @@ unittest
1129
1115
}
1130
1116
1131
1117
auto coll = client.getCollection(" test.users2" );
1132
- coll.remove ();
1118
+ coll.deleteAll ();
1133
1119
coll.register! User;
1134
1120
1135
1121
User register (string name, string password)
0 commit comments