NOTE: Key structure can be different from the original key and can include Query fields also. ANY operation is not supported for Query fields.
-### Remove
+### RemoveAll
Arguments:
```go
```
Similar to find. This will remove one or more documents based on the key structure.
+### Remove
+
+Arguments:
+```go
+collection string
+key interface
+```
+This will remove one document based on the key structure. If child refrences exist for the key then the document will not be removed.
+
### Unmarshal
Data in mongo is stored as `bson` which is a compressed form of `json`. We need mongo to convert the stored `bson` data to regular `json`
`bson.Unmarshal` API is used to achieve this.
+
opts ...*options.FindOptions) (*mongo.Cursor, error)
UpdateOne(ctx context.Context, filter interface{}, update interface{},
opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
+ CountDocuments(ctx context.Context, filter interface{},
+ opts ...*options.CountOptions) (int64, error)
}
// MongoStore is an implementation of the db.Store interface
return result, nil
}
-// Remove method to remove the documet by key
-func (m *MongoStore) Remove(coll string, key Key) error {
+// RemoveAll method to removes all the documet matching key
+func (m *MongoStore) RemoveAll(coll string, key Key) error {
if !m.validateParams(coll, key) {
return pkgerrors.New("Mandatory fields are missing")
}
}
return nil
}
+
+// Remove method to remove the documet by key if no child references
+func (m *MongoStore) Remove(coll string, key Key) error {
+ if !m.validateParams(coll, key) {
+ return pkgerrors.New("Mandatory fields are missing")
+ }
+ c := getCollection(coll, m)
+ ctx := context.Background()
+ filter, err := m.findFilter(key)
+ if err != nil {
+ return err
+ }
+ count, err := c.CountDocuments(context.Background(), filter)
+ if err != nil {
+ return pkgerrors.Errorf("Error finding: %s", err.Error())
+ }
+ if count > 1 {
+ return pkgerrors.Errorf("Can't delete parent without deleting child references first")
+ }
+ _, err = c.DeleteOne(ctx, filter)
+ if err != nil {
+ return pkgerrors.Errorf("Error Deleting from database: %s", err.Error())
+ }
+ return nil
+}
+
// Find the document(s) with key and get the tag values from the document(s)
Find(coll string, key Key, tag string) ([][]byte, error)
- // Removes the document(s) matching the key
+ // Removes the document(s) matching the key if no child reference in collection
Remove(coll string, key Key) error
+
+ // Remove all the document(s) matching the key
+ RemoveAll(coll string, key Key) error
}
// CreateDBClient creates the DB client