diff --git a/generator/config/query/comment.yaml b/generator/config/query/comment.yaml index b5b0fb2..13a3446 100644 --- a/generator/config/query/comment.yaml +++ b/generator/config/query/comment.yaml @@ -11,3 +11,21 @@ arguments: name: comment type: - string +tests: + - + name: 'Attach a Comment to an Aggregation Expression' + link: 'https://www.mongodb.com/docs/manual/reference/operator/query/comment/#attach-a-comment-to-an-aggregation-expression' + pipeline: + - + $match: + x: + $gt: 0 + $comment: 'Don''t allow negative inputs.' + - + $group: + _id: + $mod: + - '$x' + - 2 + total: + $sum: '$x' diff --git a/generator/config/query/rand.yaml b/generator/config/query/rand.yaml index 809cb6b..6773ae0 100644 --- a/generator/config/query/rand.yaml +++ b/generator/config/query/rand.yaml @@ -6,3 +6,21 @@ type: encode: object description: | Generates a random float between 0 and 1. +tests: + - + name: 'Select Random Items From a Collection' + link: 'https://www.mongodb.com/docs/manual/reference/operator/query/rand/#select-random-items-from-a-collection' + pipeline: + - + $match: + district: 3 + $expr: + $lt: + - 0.5 + - + $rand: {} + - + $project: + _id: 0 + name: 1 + registered: 1 diff --git a/generator/js2yaml.html b/generator/js2yaml.html index 89e0dd5..eeaa723 100644 --- a/generator/js2yaml.html +++ b/generator/js2yaml.html @@ -124,7 +124,7 @@

Convert JS examples into Yaml

case 'boolean': return object ? ' true' : ' false'; case 'string': - return " '" + object.replace(/'/g, "\\'") + "'"; + return " '" + object.replace(/'/g, "''") + "'"; case 'number': return ' ' + object.toString(); case 'object': diff --git a/tests/Builder/Query/CommentOperatorTest.php b/tests/Builder/Query/CommentOperatorTest.php new file mode 100644 index 0000000..4b94bb5 --- /dev/null +++ b/tests/Builder/Query/CommentOperatorTest.php @@ -0,0 +1,39 @@ +assertSamePipeline(Pipelines::CommentAttachACommentToAnAggregationExpression, $pipeline); + } +} diff --git a/tests/Builder/Query/Pipelines.php b/tests/Builder/Query/Pipelines.php index 0de857e..d72636a 100644 --- a/tests/Builder/Query/Pipelines.php +++ b/tests/Builder/Query/Pipelines.php @@ -403,6 +403,41 @@ enum Pipelines: string ] JSON; + /** + * Attach a Comment to an Aggregation Expression + * + * @see https://www.mongodb.com/docs/manual/reference/operator/query/comment/#attach-a-comment-to-an-aggregation-expression + */ + case CommentAttachACommentToAnAggregationExpression = <<<'JSON' + [ + { + "$match": { + "x": { + "$gt": { + "$numberInt": "0" + } + }, + "$comment": "Don't allow negative inputs." + } + }, + { + "$group": { + "_id": { + "$mod": [ + "$x", + { + "$numberInt": "2" + } + ] + }, + "total": { + "$sum": "$x" + } + } + } + ] + JSON; + /** * Element Match * @@ -1578,6 +1613,46 @@ enum Pipelines: string ] JSON; + /** + * Select Random Items From a Collection + * + * @see https://www.mongodb.com/docs/manual/reference/operator/query/rand/#select-random-items-from-a-collection + */ + case RandSelectRandomItemsFromACollection = <<<'JSON' + [ + { + "$match": { + "district": { + "$numberInt": "3" + }, + "$expr": { + "$lt": [ + { + "$numberDouble": "0.5" + }, + { + "$rand": {} + } + ] + } + } + }, + { + "$project": { + "_id": { + "$numberInt": "0" + }, + "name": { + "$numberInt": "1" + }, + "registered": { + "$numberInt": "1" + } + } + } + ] + JSON; + /** * Perform a LIKE Match * diff --git a/tests/Builder/Query/RandOperatorTest.php b/tests/Builder/Query/RandOperatorTest.php new file mode 100644 index 0000000..9363bd2 --- /dev/null +++ b/tests/Builder/Query/RandOperatorTest.php @@ -0,0 +1,39 @@ +assertSamePipeline(Pipelines::RandSelectRandomItemsFromACollection, $pipeline); + } +}