Add filters support for an array deleting

v2
Nikita Tokarchuk 4 years ago
parent ee4bf46662
commit 08397650c6
  1. 8
      mongox/database/deletearray.go
  2. 2
      mongox/mongox.go

@ -8,10 +8,11 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/mainnika/mongox-go-driver/v2/mongox/base" "github.com/mainnika/mongox-go-driver/v2/mongox/base"
"github.com/mainnika/mongox-go-driver/v2/mongox/query"
) )
// DeleteArray removes documents list from a database by their ids // DeleteArray removes documents list from a database by their ids
func (d *Database) DeleteArray(target interface{}) (err error) { func (d *Database) DeleteArray(target interface{}, filters ...interface{}) (err error) {
targetV := reflect.ValueOf(target) targetV := reflect.ValueOf(target)
targetT := targetV.Type() targetT := targetV.Type()
@ -37,6 +38,7 @@ func (d *Database) DeleteArray(target interface{}) (err error) {
collection := d.GetCollectionOf(zeroElem.Interface()) collection := d.GetCollectionOf(zeroElem.Interface())
opts := options.Delete() opts := options.Delete()
ids := primitive.A{} ids := primitive.A{}
composed := query.Compose(filters...)
for i := 0; i < targetLen; i++ { for i := 0; i < targetLen; i++ {
elem := targetSliceV.Index(i) elem := targetSliceV.Index(i)
@ -47,7 +49,9 @@ func (d *Database) DeleteArray(target interface{}) (err error) {
return fmt.Errorf("can't delete zero elements") return fmt.Errorf("can't delete zero elements")
} }
result, err := collection.DeleteMany(d.Context(), primitive.M{"_id": primitive.M{"$in": ids}}, opts) composed.And(primitive.M{"_id": primitive.M{"$in": ids}})
result, err := collection.DeleteMany(d.Context(), composed.M(), opts)
if err != nil { if err != nil {
return return
} }

@ -22,7 +22,7 @@ type Database interface {
New(ctx context.Context) (db Database) New(ctx context.Context) (db Database)
GetCollectionOf(document interface{}) (collection *Collection) GetCollectionOf(document interface{}) (collection *Collection)
Count(target interface{}, filters ...interface{}) (count int64, err error) Count(target interface{}, filters ...interface{}) (count int64, err error)
DeleteArray(target interface{}) (err error) DeleteArray(target interface{}, filters ...interface{}) (err error)
DeleteOne(target interface{}, filters ...interface{}) (err error) DeleteOne(target interface{}, filters ...interface{}) (err error)
LoadArray(target interface{}, filters ...interface{}) (err error) LoadArray(target interface{}, filters ...interface{}) (err error)
LoadOne(target interface{}, filters ...interface{}) (err error) LoadOne(target interface{}, filters ...interface{}) (err error)

Loading…
Cancel
Save