mongous 0.1.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.adoc +296 -46
- data/README.ja.adoc +297 -47
- data/lib/mongous/base.rb +35 -2
- data/lib/mongous/document.rb +75 -32
- data/lib/mongous/extention.rb +72 -57
- data/lib/mongous/filter.rb +273 -208
- data/lib/mongous/version.rb +1 -1
- data/mongous.gemspec +1 -1
- data/sample/connect_auto_0.rb +9 -0
- data/sample/connect_auto_2.rb +2 -1
- data/sample/connect_default_2.rb +2 -1
- data/sample/connect_environ_2.rb +2 -1
- data/sample/connect_loadfile_2.rb +2 -1
- data/sample/declare_compact_1.rb +7 -7
- data/sample/declare_label_1.rb +10 -6
- data/sample/declare_ndex_1.rb +9 -5
- data/sample/declare_ndex_2.rb +10 -6
- data/sample/declare_strict_1.rb +14 -10
- data/sample/declare_strict_2.rb +5 -3
- data/sample/{multi_docs_1.rb → multi_collection_1.rb} +0 -0
- data/sample/multi_collection_2.rb +63 -0
- data/sample/multi_collection_3.rb +26 -0
- data/sample/query_basic_1.rb +3 -1
- data/sample/query_basic_2.rb +5 -3
- data/sample/query_basic_3.rb +2 -3
- data/sample/query_basic_4.rb +3 -4
- data/sample/query_basic_5.rb +2 -3
- data/sample/query_detail_1.rb +3 -1
- data/sample/query_detail_2.rb +3 -1
- data/sample/query_detail_3.rb +5 -3
- data/sample/{query_basic_6.rb → query_filter_1.rb} +7 -4
- data/sample/query_filter_2.rb +50 -0
- data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
- data/sample/query_select_1.rb +51 -0
- data/sample/query_skip_limit_1.rb +47 -0
- data/sample/query_skip_limit_2.rb +49 -0
- data/sample/query_sort_order_1.rb +46 -0
- data/sample/save_basic_1.rb +1 -2
- data/sample/save_basic_2.rb +1 -2
- data/sample/save_basic_3.rb +1 -2
- data/sample/save_detail_1.rb +7 -4
- data/sample/save_detail_2.rb +7 -4
- data/sample/save_detail_3.rb +11 -8
- data/sample/save_verify_1.rb +7 -4
- data/sample/save_verify_3.rb +1 -1
- data/sample/save_verify_5.rb +3 -3
- data/sample/save_verify_6.rb +3 -3
- data/sample/save_verify_7.rb +23 -0
- data/sample/update_basic_1.rb +13 -0
- data/sample/zap_basic_2.rb +2 -2
- data/sample/zap_basic_3.rb +2 -2
- data/sample/zbenchmark_search_1.rb +110 -0
- data/sample/zbenchmark_search_2.rb +110 -0
- data/sample/zbenchmark_search_3.rb +90 -0
- metadata +19 -12
- data/sample/multi_docs_2.rb +0 -58
- data/sample/query_limit_1.rb +0 -44
- data/sample/query_order_1.rb +0 -49
- data/sample/query_projection_1.rb +0 -44
- data/sample/template_article.rb +0 -5
- data/sample/template_person.rb +0 -12
data/README.ja.adoc
CHANGED
@@ -4,10 +4,12 @@
|
|
4
4
|
|
5
5
|
== 特徴
|
6
6
|
|
7
|
-
* 組込みと標準添付を除いて、mongo driver と bson
|
8
|
-
*
|
9
|
-
*
|
10
|
-
*
|
7
|
+
* 組込みと標準添付を除いて、mongo driver と bson のみに依存する軽いライブラリである.
|
8
|
+
* ドキュメント項目の制約条件を Array, Range, Regexp, Proc インスタンスまたは基本クラスで記述できる.
|
9
|
+
* ドキュメント保存時に制約条件を満たすか検査する.
|
10
|
+
* ドキュメントの項目値設定時に制約条件を満たすか検査する.
|
11
|
+
* ドキュメントの未定義項目の内容を値または Proc で記述できる.
|
12
|
+
* ドキュメント作成時および更新時の項目内容を値または Proc で記述できる.
|
11
13
|
|
12
14
|
== 導入
|
13
15
|
|
@@ -30,17 +32,13 @@ gem 'mongous'
|
|
30
32
|
|
31
33
|
== 使い方
|
32
34
|
|
33
|
-
===
|
35
|
+
=== 簡潔な定義
|
34
36
|
|
35
37
|
[source,ruby]
|
36
38
|
----
|
37
39
|
require "mongous"
|
38
40
|
|
39
|
-
Mongous.
|
40
|
-
|
41
|
-
class Book
|
42
|
-
include Mongous::Document
|
43
|
-
end
|
41
|
+
Mongous.attach! :Book
|
44
42
|
----
|
45
43
|
|
46
44
|
=== 詳細な定義
|
@@ -54,15 +52,19 @@ Mongous.connect! ["localhost:27017"], database: "test"
|
|
54
52
|
class Book
|
55
53
|
include Mongous::Document
|
56
54
|
|
57
|
-
field :title, String,
|
55
|
+
field :title, String, :must
|
58
56
|
field :author, String
|
59
57
|
field :publisher, String
|
60
|
-
field :style, String,
|
61
|
-
field :
|
62
|
-
field :
|
63
|
-
field :
|
64
|
-
field :isbn, String,
|
65
|
-
field :lang, String,
|
58
|
+
field :style, String, %w[hardcover, softcover, paperback]
|
59
|
+
field :size, String, /[AB]\d/
|
60
|
+
field :price, Integer, (0..1_000_000)
|
61
|
+
field :page, Integer, proc{ page % 4 == 0 }
|
62
|
+
field :isbn, String, proc{ isbn? }
|
63
|
+
field :lang, String, default: "en"
|
64
|
+
field :created_at, Time, create: proc{ Time.now }
|
65
|
+
field :updated_at, Time, update: proc{ Time.now }
|
66
|
+
|
67
|
+
filter :foobar, {title: /foobar/}
|
66
68
|
|
67
69
|
verify :strict
|
68
70
|
verify do
|
@@ -84,13 +86,13 @@ end
|
|
84
86
|
book = Book.new
|
85
87
|
book.title = "title 1"
|
86
88
|
book.price = 1000
|
87
|
-
book.
|
89
|
+
book.size = "A4"
|
88
90
|
book.save
|
89
91
|
|
90
|
-
book = Book.new( title: "title 2", price: 2000,
|
92
|
+
book = Book.new( title: "title 2", price: 2000, size: "A5" )
|
91
93
|
book.save
|
92
94
|
|
93
|
-
doc = { title: "title 3", price: 3000,
|
95
|
+
doc = { title: "title 3", price: 3000, size: "A6" }
|
94
96
|
Book.create( **doc )
|
95
97
|
----
|
96
98
|
|
@@ -100,44 +102,56 @@ Book.create( **doc )
|
|
100
102
|
----
|
101
103
|
pp books = Book.all
|
102
104
|
|
103
|
-
p book = Book.
|
105
|
+
p book = Book.where( title: "title 1" ).first
|
104
106
|
|
105
|
-
books = Book.
|
107
|
+
books = Book.where( title: /title/ ).all
|
106
108
|
books.each do |book|
|
107
109
|
p book
|
108
110
|
end
|
109
111
|
|
110
|
-
Book.
|
112
|
+
Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
|
111
113
|
p book
|
112
114
|
end
|
113
115
|
|
114
|
-
Book.
|
116
|
+
Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
|
115
117
|
p book
|
116
118
|
end
|
117
119
|
|
118
|
-
filter1 = Book.
|
119
|
-
filter2 = Book.
|
120
|
-
filter3 = Book.
|
120
|
+
filter1 = Book.where( title: /title/ )
|
121
|
+
filter2 = Book.where( :foobar )
|
122
|
+
filter3 = Book.where( price: (1..2000) )
|
123
|
+
filter4 = Book.where( size: ["A4","A5"] )
|
124
|
+
|
121
125
|
Book.not( filter1 ).each do |book|
|
122
126
|
p book
|
123
127
|
end
|
124
|
-
Book.and( filter1,
|
128
|
+
Book.and( filter1, filter3 ).each do |book|
|
129
|
+
p book
|
130
|
+
end
|
131
|
+
Book.or( filter2, filter4 ).each do |book|
|
132
|
+
p book
|
133
|
+
end
|
134
|
+
|
135
|
+
Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
|
125
136
|
p book
|
126
137
|
end
|
127
|
-
|
138
|
+
|
139
|
+
Book.where( title: /title/ ).select( _id: 0 ).each do |book|
|
128
140
|
p book
|
129
141
|
end
|
130
142
|
|
131
|
-
Book.
|
143
|
+
Book.select( _id: 0 )[0, 5].each do |book|
|
132
144
|
p book
|
133
145
|
end
|
146
|
+
|
147
|
+
pp Book.select( :title, :price, :size )[5, 5].all
|
134
148
|
----
|
135
149
|
|
136
150
|
=== ドキュメント更新
|
137
151
|
|
138
152
|
[source,ruby]
|
139
153
|
----
|
140
|
-
book = Book.
|
154
|
+
book = Book.where( title: "title 1" ).first
|
141
155
|
book.title = "title 1 [update]"
|
142
156
|
book.save
|
143
157
|
----
|
@@ -146,7 +160,7 @@ book.save
|
|
146
160
|
|
147
161
|
[source,ruby]
|
148
162
|
----
|
149
|
-
book = Book.
|
163
|
+
book = Book.where( title: "title 1" ).first
|
150
164
|
book.delete
|
151
165
|
----
|
152
166
|
|
@@ -156,7 +170,7 @@ book.delete
|
|
156
170
|
|
157
171
|
[source,ruby]
|
158
172
|
----
|
159
|
-
Mongous.connect!( hosts_or_uri = nil, **
|
173
|
+
Mongous.connect!( hosts_or_uri = nil, **options )
|
160
174
|
----
|
161
175
|
|
162
176
|
* Result:
|
@@ -164,38 +178,116 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
|
|
164
178
|
|
165
179
|
* Parameter:
|
166
180
|
** hosts_or_uri: ホスト配列または URI (default: ["localhost:21017"])
|
167
|
-
**
|
181
|
+
** options: オプション
|
168
182
|
*** file: データベース構成定義ファイルへのパス
|
169
183
|
*** mode: 実行モード (default: "development")
|
170
184
|
*** database: データベース名 (default: "test")
|
171
|
-
***
|
185
|
+
*** Mongo::Client.new のその他オプション引数
|
172
186
|
|
173
187
|
=== データベースに接続する
|
174
188
|
|
175
189
|
[source,ruby]
|
176
190
|
----
|
177
|
-
Mongous.connect( hosts_or_uri = nil, **
|
191
|
+
Mongous.connect( hosts_or_uri = nil, **options )
|
178
192
|
----
|
179
193
|
|
180
194
|
* Result:
|
181
|
-
** Mongo::Client
|
195
|
+
** Mongo::Client インスタンス.
|
182
196
|
|
183
|
-
===
|
197
|
+
=== コレクション操作クラスをデフォルト設定で定義する.
|
198
|
+
|
199
|
+
[source,ruby]
|
200
|
+
----
|
201
|
+
Mongous.attach!( *names )
|
202
|
+
----
|
203
|
+
|
204
|
+
* Result:
|
205
|
+
** nil.
|
206
|
+
|
207
|
+
* Parameter:
|
208
|
+
** names: コレクション名. (String または Symbol)
|
209
|
+
|
210
|
+
=== コレクション操作クラスにドキュメントの機能を取り入れる.
|
184
211
|
|
185
212
|
[source,ruby]
|
186
213
|
----
|
187
214
|
include Mongous::Document
|
188
215
|
----
|
189
216
|
|
217
|
+
=== 別のデータベースを割り当てる.
|
218
|
+
|
219
|
+
[source,ruby]
|
220
|
+
----
|
221
|
+
self.client=( client )
|
222
|
+
----
|
223
|
+
|
224
|
+
* Result:
|
225
|
+
** Mongo::Client インスタンス.
|
226
|
+
|
227
|
+
* Parameter:
|
228
|
+
** client: Mongo::Client インスタンス.
|
229
|
+
|
230
|
+
=== Get binded database.
|
231
|
+
|
232
|
+
[source,ruby]
|
233
|
+
----
|
234
|
+
self.client
|
235
|
+
----
|
236
|
+
|
237
|
+
* Result:
|
238
|
+
** Mongo::Client インスタンス.
|
239
|
+
|
240
|
+
* Parameter:
|
241
|
+
** None.
|
242
|
+
|
243
|
+
=== 別のコレクションを割り当てる.
|
244
|
+
|
245
|
+
[source,ruby]
|
246
|
+
----
|
247
|
+
self.collection_name=( collection_name )
|
248
|
+
----
|
249
|
+
|
250
|
+
* Result:
|
251
|
+
** Collection name 文字列.
|
252
|
+
|
253
|
+
* Parameter:
|
254
|
+
** collection_name: コレクション名.
|
255
|
+
|
256
|
+
=== Get binded collection name.
|
257
|
+
|
258
|
+
[source,ruby]
|
259
|
+
----
|
260
|
+
self.collection_name
|
261
|
+
----
|
262
|
+
|
263
|
+
* Result:
|
264
|
+
** Collection name 文字列.
|
265
|
+
|
266
|
+
* Parameter:
|
267
|
+
** None.
|
268
|
+
|
269
|
+
=== Get collection.
|
270
|
+
|
271
|
+
[source,ruby]
|
272
|
+
----
|
273
|
+
self.collection( collection_name = nil )
|
274
|
+
----
|
275
|
+
|
276
|
+
* Result:
|
277
|
+
** Mongo::Collection インスタンス.
|
278
|
+
|
279
|
+
* Parameter:
|
280
|
+
** collection_name: 一時指定するコレクション名.
|
281
|
+
|
190
282
|
=== ドキュメントの要素を定義する.
|
191
283
|
|
192
284
|
[source,ruby]
|
193
285
|
----
|
194
|
-
field(
|
286
|
+
self.field( symbol, *attrs, **items )
|
195
287
|
----
|
196
288
|
|
197
289
|
* Parameter:
|
198
|
-
**
|
290
|
+
** symbol: 項目名
|
199
291
|
** attrs: 項目属性
|
200
292
|
*** Class: 項目検証用 Class
|
201
293
|
*** Proc: 項目検証用 Proc
|
@@ -203,17 +295,20 @@ field( label, *attrs, &block )
|
|
203
295
|
*** Array: 項目検証用配列
|
204
296
|
*** Symbol: 特別な指示子
|
205
297
|
**** must: ナル値でも空文字列でもない
|
206
|
-
**
|
298
|
+
** items: 保存時の操作.
|
299
|
+
*** default: 未定義のときの値または Proc.
|
300
|
+
*** create: ドキュメントを新規保存するときの値または Proc.
|
301
|
+
*** update: ドキュメントを更新するときの値または Proc.
|
207
302
|
|
208
303
|
=== 保存や代入の前にドキュメントの要素を検証する.
|
209
304
|
|
210
305
|
[source,ruby]
|
211
306
|
----
|
212
|
-
verify( *
|
307
|
+
self.verify( *directives, &block )
|
213
308
|
----
|
214
309
|
|
215
310
|
* Parameter:
|
216
|
-
**
|
311
|
+
** directives: 条件シンボル
|
217
312
|
*** strict: 定義済み項目名であることを検証する.
|
218
313
|
** block: 各項目値を検証して真偽を返す内容を記述する.
|
219
314
|
|
@@ -221,18 +316,18 @@ verify( *syms, &block )
|
|
221
316
|
|
222
317
|
[source,ruby]
|
223
318
|
----
|
224
|
-
index( *
|
319
|
+
self.index( *symbols, **options )
|
225
320
|
----
|
226
321
|
|
227
322
|
* Parameter:
|
228
|
-
**
|
229
|
-
**
|
323
|
+
** symbols: 項目名
|
324
|
+
** options: Mongo::Collection#indexes() のオプション.
|
230
325
|
|
231
326
|
=== 項目値がナル値でも空文字列でもないことを検証する.
|
232
327
|
|
233
328
|
[source,ruby]
|
234
329
|
----
|
235
|
-
having?( label )
|
330
|
+
self.having?( label )
|
236
331
|
----
|
237
332
|
|
238
333
|
* Result:
|
@@ -241,6 +336,161 @@ having?( label )
|
|
241
336
|
* Parameter:
|
242
337
|
** label: メソッド呼び出しする項目名.
|
243
338
|
|
339
|
+
=== 検索条件に名前をつける.
|
340
|
+
|
341
|
+
[source,ruby]
|
342
|
+
----
|
343
|
+
self.filter( symbol, filter_or_cond )
|
344
|
+
----
|
345
|
+
|
346
|
+
* Parameter:
|
347
|
+
** symbol: 項目名
|
348
|
+
** filter_or_cond: フィルタまたは検索条件
|
349
|
+
|
350
|
+
=== 出力項目を選択する.
|
351
|
+
|
352
|
+
[source,ruby]
|
353
|
+
----
|
354
|
+
Mongous::Document.select( *keys, **kwargs )
|
355
|
+
Mongous::Filter#select( *keys, **kwargs )
|
356
|
+
----
|
357
|
+
|
358
|
+
* Result:
|
359
|
+
** Mongous::Filter instance.
|
360
|
+
|
361
|
+
* Parameter:
|
362
|
+
** keys: 項目名
|
363
|
+
** kwargs: 項目名と項目値.
|
364
|
+
|
365
|
+
=== 検索条件.
|
366
|
+
|
367
|
+
[source,ruby]
|
368
|
+
----
|
369
|
+
Mongous::Document.where( filter = nil, **conditions )
|
370
|
+
Mongous::Filter#where( filter = nil, **conditions )
|
371
|
+
----
|
372
|
+
|
373
|
+
* Result:
|
374
|
+
** Mongous::Filter instance.
|
375
|
+
|
376
|
+
* Parameter:
|
377
|
+
** filter: 項目名またはフィルタインスタンス
|
378
|
+
** conditions: 検索条件
|
379
|
+
|
380
|
+
=== 否定検索条件.
|
381
|
+
|
382
|
+
[source,ruby]
|
383
|
+
----
|
384
|
+
Mongous::Document.not( filter = nil, **conditions )
|
385
|
+
Mongous::Filter#not( filter = nil, **conditions )
|
386
|
+
----
|
387
|
+
|
388
|
+
* Result:
|
389
|
+
** Mongous::Filter instance.
|
390
|
+
|
391
|
+
* Parameter:
|
392
|
+
** filter: 項目名またはフィルタインスタンス
|
393
|
+
** conditions: 検索条件
|
394
|
+
|
395
|
+
=== 論理積検索条件.
|
396
|
+
|
397
|
+
[source,ruby]
|
398
|
+
----
|
399
|
+
Mongous::Document.and( *filters )
|
400
|
+
Mongous::Filter#and( *filters )
|
401
|
+
----
|
402
|
+
|
403
|
+
* Result:
|
404
|
+
** Mongous::Filter instance.
|
405
|
+
|
406
|
+
* Parameter:
|
407
|
+
** filters: 項目名またはフィルタインスタンス
|
408
|
+
|
409
|
+
=== 論理和検索条件.
|
410
|
+
|
411
|
+
[source,ruby]
|
412
|
+
----
|
413
|
+
Mongous::Document.or( *filters )
|
414
|
+
Mongous::Filter#or( *filters )
|
415
|
+
----
|
416
|
+
|
417
|
+
* Result:
|
418
|
+
** Mongous::Filter instance.
|
419
|
+
|
420
|
+
* Parameter:
|
421
|
+
** filters: 項目名またはフィルタインスタンス
|
422
|
+
|
423
|
+
=== ドキュメントを作成.
|
424
|
+
|
425
|
+
[source,ruby]
|
426
|
+
----
|
427
|
+
Mongous::Document.create( **doc )
|
428
|
+
----
|
429
|
+
|
430
|
+
* Result:
|
431
|
+
** nil.
|
432
|
+
|
433
|
+
* Parameter:
|
434
|
+
** doc: キーワード引数.
|
435
|
+
|
436
|
+
=== ドキュメントを保存.
|
437
|
+
|
438
|
+
[source,ruby]
|
439
|
+
----
|
440
|
+
Mongous::Document#save
|
441
|
+
----
|
442
|
+
|
443
|
+
* Result:
|
444
|
+
** nil.
|
445
|
+
|
446
|
+
=== ドキュメントをHashに変換.
|
447
|
+
|
448
|
+
[source,ruby]
|
449
|
+
----
|
450
|
+
Mongous::Document#to_hash
|
451
|
+
----
|
452
|
+
|
453
|
+
* Result:
|
454
|
+
** Hash object.
|
455
|
+
|
456
|
+
=== ドキュメントをJSONに変換.
|
457
|
+
|
458
|
+
[source,ruby]
|
459
|
+
----
|
460
|
+
Mongous::Document#to_json
|
461
|
+
----
|
462
|
+
|
463
|
+
* Result:
|
464
|
+
** JSON String.
|
465
|
+
|
466
|
+
=== ドキュメントの項目値を読む.
|
467
|
+
|
468
|
+
[source,ruby]
|
469
|
+
----
|
470
|
+
Mongous::Document#[]( field_name )
|
471
|
+
Mongous::Document#field_name
|
472
|
+
----
|
473
|
+
|
474
|
+
* Result:
|
475
|
+
** 項目値.
|
476
|
+
|
477
|
+
* Parameter:
|
478
|
+
** field_name: 項目名.
|
479
|
+
|
480
|
+
=== ドキュメントの項目値を書く.
|
481
|
+
|
482
|
+
[source,ruby]
|
483
|
+
----
|
484
|
+
Mongous::Document#[]=( field_name, field_value )
|
485
|
+
Mongous::Document#field_name = field_value
|
486
|
+
----
|
487
|
+
|
488
|
+
* Result:
|
489
|
+
** 項目値.
|
490
|
+
|
491
|
+
* Parameter:
|
492
|
+
** field_name: 項目名.
|
493
|
+
** field_value: 項目値.
|
244
494
|
|
245
495
|
== 貢献
|
246
496
|
|