mongo_ql 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +20 -0
- data/Gemfile +3 -0
- data/README.md +83 -56
- data/lib/mongo_ql/collection_operators.rb +6 -0
- data/lib/mongo_ql/expression/value_node.rb +1 -1
- data/lib/mongo_ql/stage/group.rb +1 -1
- data/lib/mongo_ql/version.rb +1 -1
- data/mongo_ql.gemspec +1 -1
- metadata +5 -4
- data/Untitled-1 +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b2d85bf237fe01d81f3c4f07053f2f536220bee0142678693be766e9f1f4caf
|
4
|
+
data.tar.gz: 783abc9d2dc5ca65fed6064ee10978f7cd8d59faa41dfe58f9fb4d57b847fed7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b57f65ea24dcad07a359ae7ed500f5be06679495b1ce9b0c3d67b57d25188fb1a34de044997b55b8a4fbc37b5c9de59ab66cd6da5c73148e2fd3326c370ffd4c
|
7
|
+
data.tar.gz: 13e6bd11d96fa929b6ce186ddc21457c570cbc4a958e03b383c1a5f3a97b86df5fd2e7d634ac8553addd01b7f7d3dfc6b279e5e75628abb24c6b7bd19aaed48f
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v1
|
12
|
+
- name: Set up Ruby 2.6
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- name: Build and test with Rake
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rake test
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,33 +1,35 @@
|
|
1
|
+
# MongoQL
|
2
|
+
[![Gem](https://img.shields.io/gem/v/mongo_ql.svg?style=flat)](http://rubygems.org/gems/mongo_ql "View this project in Rubygems")
|
3
|
+
[![Actions Status](https://github.com/dingxizheng/mongo_ql/workflows/Ruby/badge.svg)](https://github.com/dingxizheng/mongo_ql/actions)
|
4
|
+
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
Install from RubyGems by adding it to your `Gemfile`, then bundling.
|
1
8
|
|
2
|
-
# Query DSL
|
3
9
|
```ruby
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
Order.where { tax > (shipping / 2) }
|
11
|
-
#=> Order.where(tax: { "$gt": { "$divide": [ "$shipping", 2]}})
|
12
|
-
Order.where { total >= If(currency == "CAD", 100, 80) }
|
13
|
-
#=> Order.where(total: { "$cond": { if: { "$eq": ["$currency", "CAD"]}, then: 100, else: 80 }})
|
10
|
+
# Gemfile
|
11
|
+
gem 'mongo_ql'
|
12
|
+
```
|
13
|
+
|
14
|
+
```
|
15
|
+
$ bundle install
|
14
16
|
```
|
15
17
|
|
16
|
-
|
18
|
+
## Aggregation Pipeline DSL
|
17
19
|
```ruby
|
18
|
-
|
19
|
-
|
20
|
+
MongoQL.compose do
|
21
|
+
lookup customers,
|
20
22
|
on: customer_id == _id.to_id,
|
21
23
|
as: customers
|
22
24
|
|
23
|
-
|
25
|
+
lookup shippings, :as => shippings do |doc|
|
24
26
|
match order_id == doc._id,
|
25
27
|
status == :shipped
|
26
28
|
end
|
27
29
|
|
28
|
-
|
30
|
+
where province == "ON"
|
29
31
|
|
30
|
-
project _id,
|
32
|
+
project :_id,
|
31
33
|
total,
|
32
34
|
customer => customers.name,
|
33
35
|
tax => total * tax_rate
|
@@ -36,47 +38,72 @@ Order.all.mongo_ql do
|
|
36
38
|
total => total.sum,
|
37
39
|
total_tax => tax.sum * 5
|
38
40
|
|
39
|
-
|
41
|
+
sort age.dsc
|
40
42
|
end
|
43
|
+
```
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
45
|
+
## The above aggregation is equivalent to the following mognodb pipeline
|
46
|
+
```json
|
47
|
+
[{
|
48
|
+
"$lookup": {
|
49
|
+
"from": "customers",
|
50
|
+
"as": "customers",
|
51
|
+
"localField": "customer_id",
|
52
|
+
"foreignField": {
|
53
|
+
"$toString": {
|
54
|
+
"$toObjectId": "$_id"
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}, {
|
59
|
+
"$lookup": {
|
60
|
+
"from": "shippings",
|
61
|
+
"as": "shippings",
|
62
|
+
"pipeline": [{
|
58
63
|
"$match": {
|
59
|
-
|
60
|
-
|
64
|
+
"$expr": {
|
65
|
+
"$and": [{
|
66
|
+
"$eq": ["$order_id", "$$var__id"]
|
67
|
+
}, {
|
68
|
+
"$eq": ["$status", "shipped"]
|
69
|
+
}]
|
70
|
+
}
|
61
71
|
}
|
62
|
-
}]
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
}
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
72
|
+
}],
|
73
|
+
"let": {
|
74
|
+
"var__id": "$_id"
|
75
|
+
}
|
76
|
+
}
|
77
|
+
}, {
|
78
|
+
"$match": {
|
79
|
+
"$expr": {
|
80
|
+
"$eq": ["$province", "ON"]
|
81
|
+
}
|
82
|
+
}
|
83
|
+
}, {
|
84
|
+
"$project": {
|
85
|
+
"_id": 1,
|
86
|
+
"total": 1,
|
87
|
+
"customer": "customers",
|
88
|
+
"tax": {
|
89
|
+
"$multiply": ["$total", "$tax_rate"]
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}, {
|
93
|
+
"$group": {
|
94
|
+
"_id": "$customer",
|
95
|
+
"total": {
|
96
|
+
"$sum": "$total"
|
97
|
+
},
|
98
|
+
"total_tax": {
|
99
|
+
"$multiply": [{
|
100
|
+
"$sum": "$tax"
|
101
|
+
}, 5]
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}, {
|
105
|
+
"$sort": {
|
106
|
+
"age": -1
|
107
|
+
}
|
108
|
+
}]
|
82
109
|
```
|
@@ -32,6 +32,12 @@ module MongoQL
|
|
32
32
|
}
|
33
33
|
end
|
34
34
|
|
35
|
+
def concat_arrays(*expressions)
|
36
|
+
Expression::MethodCall.new "$concatArrays", self, ast_template: -> (target, **_args) {
|
37
|
+
[target, *expressions]
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
35
41
|
def map(&block)
|
36
42
|
evaled_in = block.call(Expression::FieldNode.new("$item"))
|
37
43
|
Expression::MethodCall.new "$map", self, ast_template: -> (target, **_args) {
|
data/lib/mongo_ql/stage/group.rb
CHANGED
data/lib/mongo_ql/version.rb
CHANGED
data/mongo_ql.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ["Xizheng Ding"]
|
11
11
|
spec.email = ["dingxizheng@gamil.com"]
|
12
12
|
|
13
|
-
spec.summary = "A DSL for building
|
13
|
+
spec.summary = "A DSL for building MongoDB query in a more natual way"
|
14
14
|
spec.homepage = "https://github.com/dingxizheng/mongo_ql"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_ql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xizheng Ding
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -45,13 +45,14 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- ".github/workflows/ruby.yml"
|
48
49
|
- ".gitignore"
|
49
50
|
- ".rubocop.yml"
|
50
51
|
- ".vscode/launch.json"
|
52
|
+
- Gemfile
|
51
53
|
- LICENSE.txt
|
52
54
|
- README.md
|
53
55
|
- Rakefile
|
54
|
-
- Untitled-1
|
55
56
|
- bin/console
|
56
57
|
- bin/setup
|
57
58
|
- debug.sh
|
@@ -106,5 +107,5 @@ requirements: []
|
|
106
107
|
rubygems_version: 3.0.3
|
107
108
|
signing_key:
|
108
109
|
specification_version: 4
|
109
|
-
summary: A DSL for building
|
110
|
+
summary: A DSL for building MongoDB query in a more natual way
|
110
111
|
test_files: []
|
data/Untitled-1
DELETED