diff --git a/how-it-works.md b/how-it-works.md index 9f36a2b5..51a41fcd 100644 --- a/how-it-works.md +++ b/how-it-works.md @@ -37,7 +37,7 @@ The `=` operator then pipes the 'root' context through the `rhs` expression of ` dog ``` -Both sides have now been evaluated, so now the operator copies across the value from the RHS (`.b`) to the the LHS (`.a`), and it returns the now updated context: +Both sides have now been evaluated, so now the operator copies across the value from the RHS (`.b`) to the LHS (`.a`), and it returns the now updated context: ```yaml a: dog diff --git a/operators/alternative-default-value.md b/operators/alternative-default-value.md index a1a3208a..1c70d0cb 100644 --- a/operators/alternative-default-value.md +++ b/operators/alternative-default-value.md @@ -79,3 +79,36 @@ will output cat ``` +## Update or create - entity exists +This initialises `a` if it's not present + +Given a sample.yml file of: +```yaml +a: 1 +``` +then +```bash +yq '(.a // (.a = 0)) += 1' sample.yml +``` +will output +```yaml +a: 2 +``` + +## Update or create - entity does not exist +This initialises `a` if it's not present + +Given a sample.yml file of: +```yaml +b: camel +``` +then +```bash +yq '(.a // (.a = 0)) += 1' sample.yml +``` +will output +```yaml +b: camel +a: 1 +``` +