diff --git a/README.md b/README.md index 83c4f75..d38d2a0 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ You can walkaround to find out more commands, but here are some #### You might using comparision queries like this: ```go -filter := moper.D{}. +filter := moper.NewD(). Equal("damage", 10). EqualLess("health", 100). Greater("speed", 20.1) @@ -41,7 +41,7 @@ filter := moper.Init( #### Update commands ```go -update := moper.D{}.Set( +update := moper.NewD().Set( moper.P{"damage", 10}, moper.P{"health", 1}, ).Inc( @@ -53,10 +53,10 @@ Support simple aggregation: ```go intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} -matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr)) -groupStage := moper.D{}.Group( +matchStage := moper.NewD().MatchD(moper.NewD().InArray("damage", intArr)) +groupStage := moper.NewD().Group( moper.P{K: "_id", V: nil}, - moper.P{K: "total", V: moper.D{}.Sum("damage")}, + moper.P{K: "total", V: moper.NewD().Sum("damage")}, ) req := &mocom.AggregationRequest[Hero]{ @@ -92,8 +92,8 @@ type Model interface { } // Hero is Model -filter := moper.D{}.Equal("damage", -i) -update := moper.D{}.Set(moper.P{K: "damage", V: i}) +filter := moper.NewD().Equal("damage", -i) +update := moper.NewD().Set(moper.P{K: "damage", V: i}) result, err := mocom.UpdateMany[Hero](ctx, filter, update) ``` diff --git a/internal/mopertest/aggregation_test.go b/internal/mopertest/aggregation_test.go index d94bf85..bee50e5 100644 --- a/internal/mopertest/aggregation_test.go +++ b/internal/mopertest/aggregation_test.go @@ -14,14 +14,14 @@ import ( func TestAggregation(t *testing.T) { intArr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} - matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr)) - groupStage := moper.D{}.Group( + matchStage := moper.NewD().MatchD(*moper.NewD().InArray("damage", intArr)) + groupStage := moper.NewD().Group( moper.P{K: "_id", V: nil}, - moper.P{K: "total", V: moper.D{}.Sum("damage")}, + moper.P{K: "total", V: moper.NewD().Sum("damage")}, ) req := &mocom.AggregationRequest[Hero]{ - Pipeline: []moper.D{matchStage, groupStage}, + Pipeline: []*moper.D{matchStage, groupStage}, Options: []*options.AggregateOptions{}, } result, err := mocom.Aggregate(context.Background(), req) @@ -43,18 +43,18 @@ func TestAggregation(t *testing.T) { func TestLookup(t *testing.T) { intArr := []int{1} - matchStage := moper.D{}.MatchD(moper.D{}.InArray("damage", intArr)) + matchStage := moper.NewD().MatchD(*moper.NewD().InArray("damage", intArr)) - lookupStage := moper.D{}.LookUp(). + lookupStage := moper.NewD().LookUp(). From(Weapon{}.CollName()). LocalField("damage"). ForeignField("damage"). As("weapon") - unwindStage := moper.D{}.Equal("$unwind", moper.D{}.Equal("path", "$weapon").Equal("preserveNullAndEmptyArrays", false)) + unwindStage := moper.NewD().Equal("$unwind", moper.NewD().Equal("path", "$weapon").Equal("preserveNullAndEmptyArrays", false)) req := &mocom.AggregationRequest[Hero]{ - Pipeline: []moper.D{matchStage, lookupStage.D(), unwindStage}, + Pipeline: []*moper.D{matchStage, lookupStage.D(), unwindStage}, Options: []*options.AggregateOptions{}, } result, err := mocom.Aggregate(context.Background(), req) diff --git a/internal/mopertest/comparision_test.go b/internal/mopertest/comparision_test.go index f49f5ce..6cdf477 100644 --- a/internal/mopertest/comparision_test.go +++ b/internal/mopertest/comparision_test.go @@ -11,7 +11,7 @@ import ( func TestEquals(t *testing.T) { ctx := context.Background() for i := 0; i < ROUND; i++ { - filter := moper.D{}.Equal("damage", i+1) + filter := moper.NewD().Equal("damage", i+1) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestEquals]:", err) @@ -27,7 +27,7 @@ func TestNotEquals(t *testing.T) { for i := 0; i < ROUND; i++ { num := i + 1 - filter := moper.D{}.NotEqual("damage", num) + filter := moper.NewD().NotEqual("damage", num) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestNotEquals]:", err) @@ -47,7 +47,7 @@ func TestIn(t *testing.T) { for j := 0; j < i; j++ { dmg2 := j + 1 - filter := moper.D{}.InEll("damage", dmg1, dmg2) + filter := moper.NewD().InEll("damage", dmg1, dmg2) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestIn]", err) @@ -67,7 +67,7 @@ func TestNotIn(t *testing.T) { for j := 0; j < i; j++ { dmg2 := j + 1 - filter := moper.D{}.NotInEll("damage", dmg1, dmg2) + filter := moper.NewD().NotInEll("damage", dmg1, dmg2) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestNotIn]:", err) @@ -84,7 +84,7 @@ func TestLess(t *testing.T) { for i := 0; i <= ROUND; i++ { num := i * (i - 1) / 2 - filter := moper.D{}.Less("damage", i) + filter := moper.NewD().Less("damage", i) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestLess]:", err) @@ -100,7 +100,7 @@ func TestEqualLess(t *testing.T) { for i := 0; i <= ROUND; i++ { num := i * (i + 1) / 2 - filter := moper.D{}.EqualLess("damage", i) + filter := moper.NewD().EqualLess("damage", i) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestEqualLess]:", err) @@ -116,7 +116,7 @@ func TestGreater(t *testing.T) { for i := 0; i <= ROUND; i++ { num := TOTAL - i*(i+1)/2 - filter := moper.D{}.Greater("damage", i) + filter := moper.NewD().Greater("damage", i) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestGreater]:", err) @@ -133,7 +133,7 @@ func TestEqualGreater(t *testing.T) { for i := 0; i <= ROUND; i++ { num := TOTAL - i*(i-1)/2 - filter := moper.D{}.EqualGreater("damage", i) + filter := moper.NewD().EqualGreater("damage", i) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestEqualGreater]:", err) diff --git a/internal/mopertest/element_test.go b/internal/mopertest/element_test.go index 91ebf50..35f3583 100644 --- a/internal/mopertest/element_test.go +++ b/internal/mopertest/element_test.go @@ -10,14 +10,14 @@ import ( func TestExists(t *testing.T) { ctx := context.Background() - filter := moper.D{}.Exists("omit", true) + filter := moper.NewD().Exists("omit", true) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestExists]", err) } else if count != int64(ROUND) { t.Error("[TestExists]", count, "!=", int64(ROUND)) } - filter = moper.D{}.Exists("omit", false) + filter = moper.NewD().Exists("omit", false) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestExists]", err) } else if count != int64(ROUND*(ROUND-1)/2) { diff --git a/internal/mopertest/logical_test.go b/internal/mopertest/logical_test.go index d3336b8..5487dd8 100644 --- a/internal/mopertest/logical_test.go +++ b/internal/mopertest/logical_test.go @@ -13,9 +13,9 @@ func TestOr(t *testing.T) { for i := 1; i < ROUND; i++ { for j := i + 1; j <= ROUND; j++ { - filter := moper.D{}.Or( - moper.D{}.Equal("damage", i), - moper.D{}.Equal("damage", j), + filter := moper.NewD().Or( + *moper.NewD().Equal("damage", i), + *moper.NewD().Equal("damage", j), ) if count, err := mocom.Count[Hero](ctx, filter); err != nil { t.Error("[TestOr]", err) diff --git a/internal/mopertest/transaction_test.go b/internal/mopertest/transaction_test.go index 7125606..60f564a 100644 --- a/internal/mopertest/transaction_test.go +++ b/internal/mopertest/transaction_test.go @@ -24,8 +24,8 @@ func TestTransactionSuccess(t *testing.T) { Options: &options.TransactionOptions{}, Func: func(ctx mongo.SessionContext) (interface{}, error) { // update damage x to y - filter := moper.D{}.Equal("damage", x) - update := moper.D{}.Set(moper.P{K: "damage", V: y}) + filter := moper.NewD().Equal("damage", x) + update := moper.NewD().Set(moper.P{K: "damage", V: y}) _, err := mocom.UpdateMany[Hero](ctx, filter, update) if err != nil { @@ -34,8 +34,8 @@ func TestTransactionSuccess(t *testing.T) { } // update damage y to z - filter2 := moper.D{}.Equal("damage", y) - update2 := moper.D{}.Set(moper.P{K: "damage", V: z}) + filter2 := moper.NewD().Equal("damage", y) + update2 := moper.NewD().Set(moper.P{K: "damage", V: z}) result2, err := mocom.UpdateMany[Hero](ctx, filter2, update2) if err != nil { @@ -52,7 +52,7 @@ func TestTransactionSuccess(t *testing.T) { } // get all hero has damage x or y - filter := moper.D{}.InEll("damage", x, y) + filter := moper.NewD().InEll("damage", x, y) count, err := mocom.Count[Hero](ctx, filter) if err != nil { t.Error(err) @@ -61,7 +61,7 @@ func TestTransactionSuccess(t *testing.T) { } // get all hero has damage z - filter = moper.D{}.Equal("damage", z) + filter = moper.NewD().Equal("damage", z) count, err = mocom.Count[Hero](ctx, filter) if err != nil { t.Error(err) @@ -85,8 +85,8 @@ func TestTransactionFailed(t *testing.T) { Options: &options.TransactionOptions{}, Func: func(ctx mongo.SessionContext) (interface{}, error) { // update damage x to y - filter := moper.D{}.Equal("damage", x) - update := moper.D{}.Set(moper.P{K: "damage", V: y}) + filter := moper.NewD().Equal("damage", x) + update := moper.NewD().Set(moper.P{K: "damage", V: y}) _, err := mocom.UpdateMany[Hero](ctx, filter, update) if err != nil { @@ -95,8 +95,8 @@ func TestTransactionFailed(t *testing.T) { } // update damage y to z - filter2 := moper.D{}.Equal("damage", y) - update2 := moper.D{}.Set(moper.P{K: "damage", V: z}) + filter2 := moper.NewD().Equal("damage", y) + update2 := moper.NewD().Set(moper.P{K: "damage", V: z}) _, err = mocom.UpdateMany[Hero](ctx, filter2, update2) if err != nil { @@ -113,7 +113,7 @@ func TestTransactionFailed(t *testing.T) { } // get all hero has damage x or y - filter := moper.D{}.InEll("damage", x, y) + filter := moper.NewD().InEll("damage", x, y) count, err := mocom.Count[Hero](ctx, filter) if err != nil { t.Error(err) @@ -122,7 +122,7 @@ func TestTransactionFailed(t *testing.T) { } // get all hero has damage z - filter = moper.D{}.Equal("damage", z) + filter = moper.NewD().Equal("damage", z) count, err = mocom.Count[Hero](ctx, filter) if err != nil { t.Error(err) diff --git a/internal/mopertest/update_test.go b/internal/mopertest/update_test.go index d99f7e5..6eed1d3 100644 --- a/internal/mopertest/update_test.go +++ b/internal/mopertest/update_test.go @@ -14,10 +14,10 @@ func TestSet(t *testing.T) { // change all damages to negative for i := 1; i <= ROUND; i++ { - filter := moper.D{}.Equal("damage", i) - update := moper.D{}.Set(moper.P{K: "damage", V: -i}) + filter := moper.NewD().Equal("damage", i) + update := moper.NewD().Set(moper.P{K: "damage", V: -i}) - result, err := mocom.UpdateMany[Hero](ctx, filter, bson.D(update)) + result, err := mocom.UpdateMany[Hero](ctx, filter, bson.D(*update)) if err != nil { t.Error("[TestSet]", err) return @@ -31,8 +31,8 @@ func TestSet(t *testing.T) { // change damages to positive for i := 1; i <= ROUND; i++ { - filter := moper.D{}.Equal("damage", -i) - update := moper.D{}.Set(moper.P{K: "damage", V: i}) + filter := moper.NewD().Equal("damage", -i) + update := moper.NewD().Set(moper.P{K: "damage", V: i}) result, err := mocom.UpdateMany[Hero](ctx, filter, update) if err != nil { @@ -50,9 +50,9 @@ func TestSet(t *testing.T) { func TestInc(t *testing.T) { ctx := context.Background() for i := ROUND; i >= 0; i-- { - filter := moper.D{}.Equal("damage", i) + filter := moper.NewD().Equal("damage", i) - update := moper.D{}.Inc(moper.P{K: "damage", V: i}) + update := moper.NewD().Inc(moper.P{K: "damage", V: i}) result, err := mocom.UpdateMany[Hero](ctx, filter, update) if err != nil { @@ -67,8 +67,8 @@ func TestInc(t *testing.T) { } for i := 1; i <= ROUND; i++ { - filter := moper.D{}.Equal("damage", i*2) - update := moper.D{}.Inc(moper.P{ + filter := moper.NewD().Equal("damage", i*2) + update := moper.NewD().Inc(moper.P{ K: "damage", V: -i, }) @@ -89,8 +89,8 @@ func TestInc(t *testing.T) { func TestPush(t *testing.T) { ctx := context.Background() - filter := moper.D{}.Equal("damage", ROUND) - update := moper.D{}.Push(moper.P{K: "skillIds", V: 6}) + filter := moper.NewD().Equal("damage", ROUND) + update := moper.NewD().Push(moper.P{K: "skillIds", V: 6}) result, err := mocom.UpdateMany[Hero](ctx, filter, update) if err != nil { @@ -98,7 +98,7 @@ func TestPush(t *testing.T) { return } - filter2 := moper.D{}.Equal("skillIds", []int{1, 2, 3, 4, 5, 6}) + filter2 := moper.NewD().Equal("skillIds", []int{1, 2, 3, 4, 5, 6}) if count, err := mocom.Count[Hero](ctx, filter2); err != nil { t.Error("[TestPush]", err) diff --git a/mocom/mocom.go b/mocom/mocom.go index 82a765f..4ddd65d 100644 --- a/mocom/mocom.go +++ b/mocom/mocom.go @@ -40,7 +40,7 @@ func Aggregate[T Model](ctx context.Context, req *AggregationRequest[T]) (res [] // Flush clears all records of collection and return number of deleted records func Flush[T Model](ctx context.Context) (int64, error) { var t T - result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.D{}) + result, err := db.Collection(t.CollName()).DeleteMany(ctx, moper.NewD()) if err != nil { return 0, err } diff --git a/mocom/model.go b/mocom/model.go index 064dc69..ee1dafd 100644 --- a/mocom/model.go +++ b/mocom/model.go @@ -24,7 +24,7 @@ func (id *ID) SetID(t interface{}) { } type AggregationRequest[T Model] struct { - Pipeline []moper.D + Pipeline []*moper.D Options []*options.AggregateOptions } diff --git a/moper/aggregate.go b/moper/aggregate.go index 1c766b0..1e5b4a7 100644 --- a/moper/aggregate.go +++ b/moper/aggregate.go @@ -1,21 +1,21 @@ package moper -func (d D) Match(pairs ...P) D { +func (d *D) Match(pairs ...P) *D { return d.Equal("$match", toPair(pairs)) } -func (d D) MatchD(pair D) D { +func (d *D) MatchD(pair D) *D { return d.Equal("$match", pair) } -func (d D) Group(pairs ...P) D { +func (d *D) Group(pairs ...P) *D { return d.Equal("$group", toPair(pairs)) } -func (d D) GroupD(pair D) D { +func (d *D) GroupD(pair D) *D { return d.Equal("$group", pair) } -func (d D) Sum(fieldName string) D { +func (d *D) Sum(fieldName string) *D { return d.Equal("$sum", "$"+fieldName) } diff --git a/moper/aggregate_lookup.go b/moper/aggregate_lookup.go index dab9128..d426dad 100644 --- a/moper/aggregate_lookup.go +++ b/moper/aggregate_lookup.go @@ -1,7 +1,7 @@ package moper type LU struct { - d D + d *D } // Specifies the foreign collection in the same database to join to the local collection. @@ -29,8 +29,8 @@ func (l *LU) Custom(p P) *LU { return l } -func (l *LU) D() D { - return D{}.Equal("$lookup", l.d) +func (l *LU) D() *D { + return NewD().Equal("$lookup", l.d) } // Optional, Specifies the variables to use in the pipeline stages. @@ -45,6 +45,6 @@ func (l *LU) As(field string) *LU { return l } -func (d D) LookUp() *LU { +func (d *D) LookUp() *LU { return &LU{d: d} } diff --git a/moper/comparision.go b/moper/comparision.go index 0b4cc16..d6c111a 100644 --- a/moper/comparision.go +++ b/moper/comparision.go @@ -7,64 +7,78 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) -//InEll is InEllipsis -func (d D) InEll(fieldName string, value ...interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$in": value}}) +// InEll is InEllipsis +func (d *D) InEll(fieldName string, value ...interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$in": value}}) + return d } -func (d D) InArray(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$in": value}}) +func (d *D) InArray(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$in": value}}) + return d } -//NotInEll is NotInEllipsis -func (d D) NotInEll(fieldName string, value ...interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$nin": value}}) +// NotInEll is NotInEllipsis +func (d *D) NotInEll(fieldName string, value ...interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$nin": value}}) + return d } -func (d D) NotInArray(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$nin": value}}) +func (d *D) NotInArray(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$nin": value}}) + return d } // LESS OR EQUAL -func (d D) EqualLess(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$lte": value}}) +func (d *D) EqualLess(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$lte": value}}) + return d } -func (d D) EqualLessTime(fieldName string, value time.Time) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$lte": primitive.NewDateTimeFromTime(value)}}) +func (d *D) EqualLessTime(fieldName string, value time.Time) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$lte": primitive.NewDateTimeFromTime(value)}}) + return d } -func (d D) Less(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$lt": value}}) +func (d *D) Less(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$lt": value}}) + return d } -func (d D) LessTime(fieldName string, value time.Time) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$lt": primitive.NewDateTimeFromTime(value)}}) +func (d *D) LessTime(fieldName string, value time.Time) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$lt": primitive.NewDateTimeFromTime(value)}}) + return d } // GREATER OR EQUAL -func (d D) EqualGreater(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$gte": value}}) +func (d *D) EqualGreater(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$gte": value}}) + return d } -func (d D) EqualGreaterTime(fieldName string, value time.Time) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$gte": primitive.NewDateTimeFromTime(value)}}) +func (d *D) EqualGreaterTime(fieldName string, value time.Time) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$gte": primitive.NewDateTimeFromTime(value)}}) + return d } -func (d D) Greater(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$gt": value}}) +func (d *D) Greater(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$gt": value}}) + return d } -func (d D) GreaterTime(fieldName string, value time.Time) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$gt": primitive.NewDateTimeFromTime(value)}}) +func (d *D) GreaterTime(fieldName string, value time.Time) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$gt": primitive.NewDateTimeFromTime(value)}}) + return d } // EQUAL -func (d D) NotEqual(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$ne": value}}) +func (d *D) NotEqual(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$ne": value}}) + return d } -func (d D) Equal(fieldName string, value interface{}) D { - return append(d, bson.E{Key: fieldName, Value: value}) +func (d *D) Equal(fieldName string, value interface{}) *D { + *d = append(*d, bson.E{Key: fieldName, Value: value}) + return d } diff --git a/moper/element.go b/moper/element.go index 9f75526..f769472 100644 --- a/moper/element.go +++ b/moper/element.go @@ -6,6 +6,7 @@ import "go.mongodb.org/mongo-driver/bson" // return Or(Equal(fieldName, value), Exist(fieldName, exist)) // } -func (d D) Exists(fieldName string, exist bool) D { - return append(d, bson.E{Key: fieldName, Value: bson.M{"$exists": exist}}) +func (d *D) Exists(fieldName string, exist bool) *D { + *d = append(*d, bson.E{Key: fieldName, Value: bson.M{"$exists": exist}}) + return d } diff --git a/moper/logical.go b/moper/logical.go index 4345e5c..ad9a847 100644 --- a/moper/logical.go +++ b/moper/logical.go @@ -2,6 +2,7 @@ package moper import "go.mongodb.org/mongo-driver/bson" -func (d D) Or(filters ...D) D { - return append(d, bson.E{Key: "$or", Value: filters}) +func (d *D) Or(filters ...D) *D { + *d = append(*d, bson.E{Key: "$or", Value: filters}) + return d } diff --git a/moper/model.go b/moper/model.go index 50dab00..1b71985 100644 --- a/moper/model.go +++ b/moper/model.go @@ -7,12 +7,16 @@ import ( type D primitive.D -//P is pair +// P is pair type P struct { K string V interface{} } +func NewD() *D { + return &D{} +} + func (d D) MarshalBSON() ([]byte, error) { return bson.Marshal(primitive.D(d)) } diff --git a/moper/update.go b/moper/update.go index c3b96cf..6b124fd 100644 --- a/moper/update.go +++ b/moper/update.go @@ -10,45 +10,52 @@ const ( ) // SET -func (d D) Set(pairs ...P) D { - return append(d, bson.E{Key: _set, Value: toPair(pairs)}) +func (d *D) Set(pairs ...P) *D { + *d = append(*d, bson.E{Key: _set, Value: toPair(pairs)}) + return d } -func (d D) SetD(pairs D) D { - return append(d, bson.E{Key: _set, Value: pairs}) +func (d *D) SetD(pairs D) *D { + *d = append(*d, bson.E{Key: _set, Value: pairs}) + return d } // UNSET -func (d D) Unset(keys ...string) D { +func (d *D) Unset(keys ...string) *D { res := D{} for i := range keys { res = append(res, bson.E{Key: keys[i], Value: ""}) } - return append(d, bson.E{Key: _unset, Value: res}) + *d = append(*d, bson.E{Key: _unset, Value: res}) + return d } // INC -func (d D) Inc(pairs ...P) D { +func (d *D) Inc(pairs ...P) *D { pairLen := len(pairs) updated := D{} for i := 0; i < pairLen; i++ { updated = append(updated, bson.E{Key: pairs[i].K, Value: pairs[i].V}) } - return append(d, bson.E{Key: _inc, Value: updated}) + *d = append(*d, bson.E{Key: _inc, Value: updated}) + return d } -func (d D) IncD(pairs D) D { - return append(d, bson.E{Key: _inc, Value: pairs}) +func (d *D) IncD(pairs D) *D { + *d = append(*d, bson.E{Key: _inc, Value: pairs}) + return d } // PUSH -func (d D) Push(pairs ...P) D { - return append(d, bson.E{Key: _push, Value: toPair(pairs)}) +func (d *D) Push(pairs ...P) *D { + *d = append(*d, bson.E{Key: _push, Value: toPair(pairs)}) + return d } // PUSH -func (d D) PushD(pairs D) D { - return append(d, bson.E{Key: _push, Value: pairs}) +func (d *D) PushD(pairs D) *D { + *d = append(*d, bson.E{Key: _push, Value: pairs}) + return d } func toPair(pairs []P) bson.D {