# README
Query for operational path
With "patch operations" patches can be specified in detail base on RFC6902
(test
operation is not implemented - use RSQL Parser instead)
There are five operations available:
Operation | Description |
---|---|
remove | remove the value at the target location. |
add | add a value or array to an array at the target location. |
replace | replaces the value at the target location with a new value. |
move | removes the value at a specified location and adds it to the target location. |
copy | copies the value from a specified location to the target location. |
This features are supported for MongoDB 4.2+
.
Additionally, simple rules can be set:
Policy | Description |
---|---|
DisallowPathPolicy | specifies a path that is not allowed. |
DisallowOperationOnPathPolicy | disallows specified operation on path. |
ForceTypeOnPathPolicy | forces the value of a specif path to be from given type. |
ForceRegexMatchPolicy | forces the value of a specif path to match expression. |
How to
Basic usage
import (
"github.com/StevenCyb/goquery/parser/mongo/patchoperation"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ...
var operations []patchoperation.OperationSpec
err = json.NewDecoder(req.Body).Decode(&operations)
// ...
parser := patchoperation.NewParser()
query, err := parser.Parse(operations...)
// ...
result := collection.FindOneAndUpdate(ctx, filter, query, updateOptions)
// ...
Policy usage
parser := patchoperation.NewParser(
DisallowPathPolicy{Details: "illegal ID modification", Path: "_id"},
ForceTypeOnPathPolicy{Details: "age as number", Path: "user.age", Kind: reflect.Int64},
),
# Functions
NewParser creates a new parser.
# Constants
AddOperation add a value or array to an array at the target location.
CopyOperation copies the value from a specified location to the target location.
MoveOperation removes the value at a specified location and adds it to the target location.
RemoveOperation is an operation to remove the value at the target location.
ReplaceOperation replaces the value at the target location with a new value.
# Structs
DisallowOperationOnPathPolicy disallows specified operation on path.
DisallowPathPolicy specifies a path that is not allowed.
ForceRegexMatchPolicy forces the value of a specif path to match expression.
ForceTypeOnPathPolicy forces the value of a specif path to be from given type.
OperationSpec specify an path operation.
Parser that can parse patch operation to generate mongo queries.
# Interfaces
Policy specifies the interface for an policy.