@@ -19,18 +19,23 @@ import (
19
19
"testing"
20
20
"time"
21
21
22
+ "github.com/qiniu/qmgo/operator"
22
23
"github.com/stretchr/testify/require"
23
24
"go.mongodb.org/mongo-driver/bson"
24
25
"go.mongodb.org/mongo-driver/bson/primitive"
25
26
"go.mongodb.org/mongo-driver/mongo"
26
-
27
- "github.com/qiniu/qmgo/operator"
27
+ "go.mongodb.org/mongo-driver/mongo/options"
28
28
)
29
29
30
30
type QueryTestItem struct {
31
31
Id primitive.ObjectID `bson:"_id"`
32
32
Name string `bson:"name"`
33
33
Age int `bson:"age"`
34
+
35
+ Instock []struct {
36
+ Warehouse string `bson:"warehouse"`
37
+ Qty int `bson:"qty"`
38
+ } `bson:"instock"`
34
39
}
35
40
36
41
type QueryTestItem2 struct {
@@ -679,8 +684,13 @@ func TestQuery_Apply(t *testing.T) {
679
684
docs := []interface {}{
680
685
bson.M {"_id" : id1 , "name" : "Alice" , "age" : 18 },
681
686
bson.M {"_id" : id2 , "name" : "Alice" , "age" : 19 },
682
- bson.M {"_id" : id3 , "name" : "Lucas" , "age" : 20 },
683
- }
687
+ bson.M {"_id" : id3 , "name" : "Lucas" , "age" : 20 , "instock" : []bson.M {
688
+ {"warehouse" : "B" , "qty" : 15 },
689
+ {"warehouse" : "C" , "qty" : 35 },
690
+ {"warehouse" : "E" , "qty" : 15 },
691
+ {"warehouse" : "F" , "qty" : 45 },
692
+ }}}
693
+
684
694
_ , _ = cli .InsertMany (context .Background (), docs )
685
695
686
696
var err error
@@ -829,6 +839,26 @@ func TestQuery_Apply(t *testing.T) {
829
839
ast .NoError (err )
830
840
ast .Equal ("" , res4 .Name )
831
841
ast .Equal (0 , res4 .Age )
842
+
843
+ var res5 = QueryTestItem {}
844
+ filter5 := bson.M {"name" : "Lucas" }
845
+ change5 := Change {
846
+ Update : bson.M {"$set" : bson.M {"instock.$[elem].qty" : 100 }},
847
+ ReturnNew : true ,
848
+ }
849
+ err = cli .Find (context .Background (), filter5 ).SetArrayFilters (& options.ArrayFilters {Filters : []interface {}{
850
+ bson.M {"elem.warehouse" : bson.M {"$in" : []string {"C" , "F" }}},
851
+ }}).Apply (change5 , & res5 )
852
+ ast .NoError (err )
853
+
854
+ for _ , item := range res5 .Instock {
855
+ switch item .Warehouse {
856
+ case "C" , "F" :
857
+ ast .Equal (100 , item .Qty )
858
+ case "B" , "E" :
859
+ ast .Equal (15 , item .Qty )
860
+ }
861
+ }
832
862
}
833
863
834
864
func TestQuery_BatchSize (t * testing.T ) {
@@ -854,4 +884,5 @@ func TestQuery_BatchSize(t *testing.T) {
854
884
err := cli .Find (context .Background (), bson.M {"name" : "Alice" }).BatchSize (1 ).All (& res )
855
885
ast .NoError (err )
856
886
ast .Len (res , 2 )
887
+
857
888
}
0 commit comments