mongous 0.1.5 → 0.3.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 +216 -44
- data/README.ja.adoc +217 -45
- data/lib/mongous/base.rb +30 -0
- data/lib/mongous/document.rb +75 -32
- data/lib/mongous/extention.rb +55 -46
- data/lib/mongous/filter.rb +82 -60
- data/lib/mongous/version.rb +1 -1
- 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} +8 -5
- data/sample/query_filter_2.rb +50 -0
- data/sample/{query_super_1.rb → query_find_1.rb} +0 -1
- data/sample/query_projection_1.rb +5 -8
- 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
- metadata +15 -10
- 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/template_article.rb +0 -5
- data/sample/template_person.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e316aaf87661c4fe27054cdbaef39ba7f21f24432b3752ffb1cb308894de5272
|
4
|
+
data.tar.gz: 5597f0a55efc8d1afa45e6bf9ee93ea77653d7161d12e776bc979a6543b2c94a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dc7ea514d20c8fc9ed8eaed86b4feb293df3e713a1db3acb60eb46abd61f28f0dbeeb9a68b9ac1442b6b690b3e4b12653bce102d007d912e6c08a700bd964ef
|
7
|
+
data.tar.gz: 44cfdfa61226dbfcaa40d846bfa2f00a76137b297141cbf4ea7318abdcc999271cddafd9e65bc5ea5d69745674da87aa719c2b34968d40968762d3f9e0855ced
|
data/README.adoc
CHANGED
@@ -5,9 +5,11 @@ Yet another mongo wrapper library.
|
|
5
5
|
== Features
|
6
6
|
|
7
7
|
* A light library that depends only on the mongo driver and bson, except for built-in and standard attachments.
|
8
|
-
*
|
9
|
-
*
|
10
|
-
*
|
8
|
+
* Document item constraints can be described in Array, Range, Regexp, Proc instances, or base classes.
|
9
|
+
* Check if the constraint conditions are met when saving the document.
|
10
|
+
* Check if the constraint condition is satisfied when setting the item value of the document.
|
11
|
+
* The contents of undefined items in the document can be described by value or Proc.
|
12
|
+
* Item contents at the time of document creation and update can be described by value or Proc.
|
11
13
|
|
12
14
|
== Installation
|
13
15
|
|
@@ -37,10 +39,7 @@ Or install it yourself as:
|
|
37
39
|
require "mongous"
|
38
40
|
|
39
41
|
Mongous.connect!
|
40
|
-
|
41
|
-
class Book
|
42
|
-
include Mongous::Document
|
43
|
-
end
|
42
|
+
Mongous.attach! :Book
|
44
43
|
----
|
45
44
|
|
46
45
|
=== Detail declaration
|
@@ -54,15 +53,19 @@ Mongous.connect! ["localhost:27017"], database: "test"
|
|
54
53
|
class Book
|
55
54
|
include Mongous::Document
|
56
55
|
|
57
|
-
field :title, String,
|
56
|
+
field :title, String, :must
|
58
57
|
field :author, String
|
59
58
|
field :publisher, String
|
60
|
-
field :style, String,
|
61
|
-
field :
|
62
|
-
field :
|
63
|
-
field :
|
64
|
-
field :isbn, String,
|
65
|
-
field :lang, String,
|
59
|
+
field :style, String, %w[hardcover, softcover, paperback]
|
60
|
+
field :size, String, /[AB]\d/
|
61
|
+
field :price, Integer, (0..1_000_000)
|
62
|
+
field :page, Integer, proc{ page % 4 == 0 }
|
63
|
+
field :isbn, String, proc{ isbn? }
|
64
|
+
field :lang, String, default: "en"
|
65
|
+
field :created_at, Time, create: proc{ Time.now }
|
66
|
+
field :updated_at, Time, update: proc{ Time.now }
|
67
|
+
|
68
|
+
filter :foobar, {title: /foobar/}
|
66
69
|
|
67
70
|
verify :strict
|
68
71
|
verify do
|
@@ -84,13 +87,13 @@ end
|
|
84
87
|
book = Book.new
|
85
88
|
book.title = "title 1"
|
86
89
|
book.price = 1000
|
87
|
-
book.
|
90
|
+
book.size = "A4"
|
88
91
|
book.save
|
89
92
|
|
90
|
-
book = Book.new( title: "title 2", price: 2000,
|
93
|
+
book = Book.new( title: "title 2", price: 2000, size: "A5" )
|
91
94
|
book.save
|
92
95
|
|
93
|
-
doc = { title: "title 3", price: 3000,
|
96
|
+
doc = { title: "title 3", price: 3000, size: "A6" }
|
94
97
|
Book.create( **doc )
|
95
98
|
----
|
96
99
|
|
@@ -100,44 +103,48 @@ Book.create( **doc )
|
|
100
103
|
----
|
101
104
|
pp books = Book.all
|
102
105
|
|
103
|
-
p book = Book.
|
106
|
+
p book = Book.where( title: "title 1" ).first
|
104
107
|
|
105
|
-
books = Book.
|
108
|
+
books = Book.where( title: /title/ ).all
|
106
109
|
books.each do |book|
|
107
110
|
p book
|
108
111
|
end
|
109
112
|
|
110
|
-
Book.
|
113
|
+
Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
|
111
114
|
p book
|
112
115
|
end
|
113
116
|
|
114
|
-
Book.
|
117
|
+
Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
|
115
118
|
p book
|
116
119
|
end
|
117
120
|
|
118
|
-
filter1 = Book.
|
119
|
-
filter2 = Book.
|
120
|
-
filter3 = Book.
|
121
|
+
filter1 = Book.where( title: /title/ )
|
122
|
+
filter2 = Book.where( :foobar )
|
123
|
+
filter3 = Book.where( price: (1..2000) )
|
124
|
+
filter4 = Book.where( size: ["A4","A5"] )
|
125
|
+
|
121
126
|
Book.not( filter1 ).each do |book|
|
122
127
|
p book
|
123
128
|
end
|
124
|
-
Book.and( filter1,
|
129
|
+
Book.and( filter1, filter3 ).each do |book|
|
125
130
|
p book
|
126
131
|
end
|
127
|
-
Book.or(
|
132
|
+
Book.or( filter2, filter4 ).each do |book|
|
128
133
|
p book
|
129
134
|
end
|
130
135
|
|
131
|
-
Book.find( {}, { projection: {_id: 0} } ).each do |book|
|
136
|
+
Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
|
132
137
|
p book
|
133
138
|
end
|
139
|
+
|
140
|
+
pp Book.where( title: /title/ )[0, 5].all
|
134
141
|
----
|
135
142
|
|
136
143
|
=== Update document
|
137
144
|
|
138
145
|
[source,ruby]
|
139
146
|
----
|
140
|
-
book = Book.
|
147
|
+
book = Book.where( title: "title 1" ).first
|
141
148
|
book.title = "title 1 [update]"
|
142
149
|
book.save
|
143
150
|
----
|
@@ -146,7 +153,7 @@ book.save
|
|
146
153
|
|
147
154
|
[source,ruby]
|
148
155
|
----
|
149
|
-
book = Book.
|
156
|
+
book = Book.where( title: "title 1" ).first
|
150
157
|
book.delete
|
151
158
|
----
|
152
159
|
|
@@ -156,7 +163,7 @@ book.delete
|
|
156
163
|
|
157
164
|
[source,ruby]
|
158
165
|
----
|
159
|
-
Mongous.connect!( hosts_or_uri = nil, **
|
166
|
+
Mongous.connect!( hosts_or_uri = nil, **options )
|
160
167
|
----
|
161
168
|
|
162
169
|
* Result:
|
@@ -164,56 +171,98 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
|
|
164
171
|
|
165
172
|
* Parameter:
|
166
173
|
** hosts_or_uri: Array of hosts, or URI (default: ["localhost:21017"])
|
167
|
-
**
|
174
|
+
** options: Options.
|
168
175
|
*** file: Path to database configuration file.
|
169
176
|
*** mode: Execution mode. (default: "development")
|
170
177
|
*** database: Database name. (default: "test")
|
171
|
-
***
|
178
|
+
*** Other optional arguments for Mongo::Client.new.
|
172
179
|
|
173
180
|
=== Connect database.
|
174
181
|
|
175
182
|
[source,ruby]
|
176
183
|
----
|
177
|
-
Mongous.connect( hosts_or_uri = nil, **
|
184
|
+
Mongous.connect( hosts_or_uri = nil, **options )
|
178
185
|
----
|
179
186
|
|
180
187
|
* Result:
|
181
|
-
** Mongo::Client.
|
188
|
+
** Mongo::Client instance.
|
189
|
+
|
190
|
+
=== Define collection operate class with default settings.
|
191
|
+
|
192
|
+
[source,ruby]
|
193
|
+
----
|
194
|
+
Mongous.attach!( *names )
|
195
|
+
----
|
196
|
+
|
197
|
+
* Result:
|
198
|
+
** nil.
|
182
199
|
|
183
|
-
|
200
|
+
* Parameter:
|
201
|
+
** names: Collection names. (String or Symbol)
|
202
|
+
|
203
|
+
=== Include document functions into collection operate class.
|
184
204
|
|
185
205
|
[source,ruby]
|
186
206
|
----
|
187
207
|
include Mongous::Document
|
188
208
|
----
|
189
209
|
|
210
|
+
=== Bind another database.
|
211
|
+
|
212
|
+
[source,ruby]
|
213
|
+
----
|
214
|
+
self.client=( _client )
|
215
|
+
----
|
216
|
+
|
217
|
+
* Result:
|
218
|
+
** Mongo::Client instance.
|
219
|
+
|
220
|
+
* Parameter:
|
221
|
+
** client: Mongo::Client instance.
|
222
|
+
|
223
|
+
=== Bind another collection.
|
224
|
+
|
225
|
+
[source,ruby]
|
226
|
+
----
|
227
|
+
self.collection_name=( _collection_name )
|
228
|
+
----
|
229
|
+
|
230
|
+
* Result:
|
231
|
+
** Collection name string.
|
232
|
+
|
233
|
+
* Parameter:
|
234
|
+
** collection_name: Collection name.
|
235
|
+
|
190
236
|
=== Declare document structure.
|
191
237
|
|
192
238
|
[source,ruby]
|
193
239
|
----
|
194
|
-
field(
|
240
|
+
field( symbol, *attrs, **items )
|
195
241
|
----
|
196
242
|
|
197
243
|
* Parameter:
|
198
|
-
**
|
244
|
+
** symbol: Field name.
|
199
245
|
** attrs: Field attributes.
|
200
246
|
*** Class: Class for field verification.
|
201
247
|
*** Proc: Proc for field verification.
|
202
248
|
*** Range: Range for field verification.
|
203
249
|
*** Array: Array for field verification.
|
204
|
-
*** Symbol: Special
|
250
|
+
*** Symbol: Special directive symbol.
|
205
251
|
**** must: Not nil nor empty.
|
206
|
-
**
|
252
|
+
** items: Operation when saving.
|
253
|
+
*** default: Value or proc when undefined.
|
254
|
+
*** create: Value or proc when saving a new document.
|
255
|
+
*** update: Value or proc when saving update document.
|
207
256
|
|
208
257
|
=== Verify before save or assignment action.
|
209
258
|
|
210
259
|
[source,ruby]
|
211
260
|
----
|
212
|
-
verify( *
|
261
|
+
verify( *directives, &block )
|
213
262
|
----
|
214
263
|
|
215
264
|
* Parameter:
|
216
|
-
**
|
265
|
+
** directives: Special directive symbol.
|
217
266
|
*** strict: Verify that it is a defined item name.
|
218
267
|
** block: Describe the content that verifies each item value and returns the truth.
|
219
268
|
|
@@ -221,12 +270,12 @@ verify( *syms, &block )
|
|
221
270
|
|
222
271
|
[source,ruby]
|
223
272
|
----
|
224
|
-
index( *
|
273
|
+
index( *symbols, **options )
|
225
274
|
----
|
226
275
|
|
227
276
|
* Parameter:
|
228
|
-
**
|
229
|
-
**
|
277
|
+
** symbols: Field names.
|
278
|
+
** options: Options for Mongo::Collection#indexes().
|
230
279
|
|
231
280
|
=== Verify field value is not nil nor empty.
|
232
281
|
|
@@ -241,6 +290,129 @@ having?( label )
|
|
241
290
|
* Parameter:
|
242
291
|
** label: Field label for method call.
|
243
292
|
|
293
|
+
=== Name the search condition.
|
294
|
+
|
295
|
+
[source,ruby]
|
296
|
+
----
|
297
|
+
filter( symbol, filter_or_cond )
|
298
|
+
----
|
299
|
+
|
300
|
+
* Parameter:
|
301
|
+
** symbol: Filter name.
|
302
|
+
** filter_or_cond: Filter or search criteria.
|
303
|
+
|
304
|
+
=== Search condition.
|
305
|
+
|
306
|
+
[source,ruby]
|
307
|
+
----
|
308
|
+
Collection operate class #where( filter = nil, **conditions )
|
309
|
+
----
|
310
|
+
|
311
|
+
* Result:
|
312
|
+
** Filter instance.
|
313
|
+
|
314
|
+
* Parameter:
|
315
|
+
** filter: Filter name symbol, or filter instance.
|
316
|
+
** conditions: Search criteria.
|
317
|
+
|
318
|
+
=== NOT search condition.
|
319
|
+
|
320
|
+
[source,ruby]
|
321
|
+
----
|
322
|
+
Collection operate class #not( filter = nil, **conditions )
|
323
|
+
----
|
324
|
+
|
325
|
+
* Result:
|
326
|
+
** Filter instance.
|
327
|
+
|
328
|
+
* Parameter:
|
329
|
+
** filter: Filter name symbol, or filter instance.
|
330
|
+
** conditions: Search criteria.
|
331
|
+
|
332
|
+
=== AND search condition.
|
333
|
+
|
334
|
+
[source,ruby]
|
335
|
+
----
|
336
|
+
Collection operate class #and( *filters )
|
337
|
+
----
|
338
|
+
|
339
|
+
* Result:
|
340
|
+
** Filter instance.
|
341
|
+
|
342
|
+
* Parameter:
|
343
|
+
** filters: Filter name symbol, or filter instance.
|
344
|
+
|
345
|
+
=== OR search condition.
|
346
|
+
|
347
|
+
[source,ruby]
|
348
|
+
----
|
349
|
+
Collection operate class #or( *filters )
|
350
|
+
----
|
351
|
+
|
352
|
+
* Result:
|
353
|
+
** Filter instance.
|
354
|
+
|
355
|
+
* Parameter:
|
356
|
+
** filters: Field name symbol, or filter instance.
|
357
|
+
|
358
|
+
=== Save document.
|
359
|
+
|
360
|
+
[source,ruby]
|
361
|
+
----
|
362
|
+
Document operate object #save
|
363
|
+
----
|
364
|
+
|
365
|
+
* Result:
|
366
|
+
** nil.
|
367
|
+
|
368
|
+
=== Convert document to Hash.
|
369
|
+
|
370
|
+
[source,ruby]
|
371
|
+
----
|
372
|
+
Document operate object #to_hash
|
373
|
+
----
|
374
|
+
|
375
|
+
* Result:
|
376
|
+
** Hash object.
|
377
|
+
|
378
|
+
=== Convert document to JSON.
|
379
|
+
|
380
|
+
[source,ruby]
|
381
|
+
----
|
382
|
+
Document operate object #to_hash
|
383
|
+
----
|
384
|
+
|
385
|
+
* Result:
|
386
|
+
** JSON String.
|
387
|
+
|
388
|
+
=== Read document field.
|
389
|
+
|
390
|
+
[source,ruby]
|
391
|
+
----
|
392
|
+
Document operate object #[]( field_name )
|
393
|
+
Document operate object #field_name
|
394
|
+
----
|
395
|
+
|
396
|
+
* Result:
|
397
|
+
** field_value.
|
398
|
+
|
399
|
+
* Parameter:
|
400
|
+
** field_name: Field name.
|
401
|
+
|
402
|
+
=== Write document field.
|
403
|
+
|
404
|
+
[source,ruby]
|
405
|
+
----
|
406
|
+
Document operate object #[]=( field_name, field_value )
|
407
|
+
Document operate object #field_name = field_value
|
408
|
+
----
|
409
|
+
|
410
|
+
* Result:
|
411
|
+
** field_value.
|
412
|
+
|
413
|
+
* Parameter:
|
414
|
+
** field_name: Field name.
|
415
|
+
** field_value: Field value.
|
244
416
|
|
245
417
|
== Contributing
|
246
418
|
|
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,14 @@ 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
41
|
Mongous.connect!
|
40
|
-
|
41
|
-
class Book
|
42
|
-
include Mongous::Document
|
43
|
-
end
|
42
|
+
Mongous.attach! :Book
|
44
43
|
----
|
45
44
|
|
46
45
|
=== 詳細な定義
|
@@ -54,15 +53,19 @@ Mongous.connect! ["localhost:27017"], database: "test"
|
|
54
53
|
class Book
|
55
54
|
include Mongous::Document
|
56
55
|
|
57
|
-
field :title, String,
|
56
|
+
field :title, String, :must
|
58
57
|
field :author, String
|
59
58
|
field :publisher, String
|
60
|
-
field :style, String,
|
61
|
-
field :
|
62
|
-
field :
|
63
|
-
field :
|
64
|
-
field :isbn, String,
|
65
|
-
field :lang, String,
|
59
|
+
field :style, String, %w[hardcover, softcover, paperback]
|
60
|
+
field :size, String, /[AB]\d/
|
61
|
+
field :price, Integer, (0..1_000_000)
|
62
|
+
field :page, Integer, proc{ page % 4 == 0 }
|
63
|
+
field :isbn, String, proc{ isbn? }
|
64
|
+
field :lang, String, default: "en"
|
65
|
+
field :created_at, Time, create: proc{ Time.now }
|
66
|
+
field :updated_at, Time, update: proc{ Time.now }
|
67
|
+
|
68
|
+
filter :foobar, {title: /foobar/}
|
66
69
|
|
67
70
|
verify :strict
|
68
71
|
verify do
|
@@ -84,13 +87,13 @@ end
|
|
84
87
|
book = Book.new
|
85
88
|
book.title = "title 1"
|
86
89
|
book.price = 1000
|
87
|
-
book.
|
90
|
+
book.size = "A4"
|
88
91
|
book.save
|
89
92
|
|
90
|
-
book = Book.new( title: "title 2", price: 2000,
|
93
|
+
book = Book.new( title: "title 2", price: 2000, size: "A5" )
|
91
94
|
book.save
|
92
95
|
|
93
|
-
doc = { title: "title 3", price: 3000,
|
96
|
+
doc = { title: "title 3", price: 3000, size: "A6" }
|
94
97
|
Book.create( **doc )
|
95
98
|
----
|
96
99
|
|
@@ -100,44 +103,48 @@ Book.create( **doc )
|
|
100
103
|
----
|
101
104
|
pp books = Book.all
|
102
105
|
|
103
|
-
p book = Book.
|
106
|
+
p book = Book.where( title: "title 1" ).first
|
104
107
|
|
105
|
-
books = Book.
|
108
|
+
books = Book.where( title: /title/ ).all
|
106
109
|
books.each do |book|
|
107
110
|
p book
|
108
111
|
end
|
109
112
|
|
110
|
-
Book.
|
113
|
+
Book.where( title: /title/ ).projection( _id: 0 ).each do |book|
|
111
114
|
p book
|
112
115
|
end
|
113
116
|
|
114
|
-
Book.
|
117
|
+
Book.where( price: (1..2000), size: ["A4","A5"] ).each do |book|
|
115
118
|
p book
|
116
119
|
end
|
117
120
|
|
118
|
-
filter1 = Book.
|
119
|
-
filter2 = Book.
|
120
|
-
filter3 = Book.
|
121
|
+
filter1 = Book.where( title: /title/ )
|
122
|
+
filter2 = Book.where( :foobar )
|
123
|
+
filter3 = Book.where( price: (1..2000) )
|
124
|
+
filter4 = Book.where( size: ["A4","A5"] )
|
125
|
+
|
121
126
|
Book.not( filter1 ).each do |book|
|
122
127
|
p book
|
123
128
|
end
|
124
|
-
Book.and( filter1,
|
129
|
+
Book.and( filter1, filter3 ).each do |book|
|
125
130
|
p book
|
126
131
|
end
|
127
|
-
Book.or(
|
132
|
+
Book.or( filter2, filter4 ).each do |book|
|
128
133
|
p book
|
129
134
|
end
|
130
135
|
|
131
|
-
Book.find( {}, { projection: {_id: 0} } ).each do |book|
|
136
|
+
Book.find( { title: /title/ }, { projection: {_id: 0} } ).each do |book|
|
132
137
|
p book
|
133
138
|
end
|
139
|
+
|
140
|
+
pp Book.where( title: /title/ )[0, 5].all
|
134
141
|
----
|
135
142
|
|
136
143
|
=== ドキュメント更新
|
137
144
|
|
138
145
|
[source,ruby]
|
139
146
|
----
|
140
|
-
book = Book.
|
147
|
+
book = Book.where( title: "title 1" ).first
|
141
148
|
book.title = "title 1 [update]"
|
142
149
|
book.save
|
143
150
|
----
|
@@ -146,7 +153,7 @@ book.save
|
|
146
153
|
|
147
154
|
[source,ruby]
|
148
155
|
----
|
149
|
-
book = Book.
|
156
|
+
book = Book.where( title: "title 1" ).first
|
150
157
|
book.delete
|
151
158
|
----
|
152
159
|
|
@@ -156,7 +163,7 @@ book.delete
|
|
156
163
|
|
157
164
|
[source,ruby]
|
158
165
|
----
|
159
|
-
Mongous.connect!( hosts_or_uri = nil, **
|
166
|
+
Mongous.connect!( hosts_or_uri = nil, **options )
|
160
167
|
----
|
161
168
|
|
162
169
|
* Result:
|
@@ -164,38 +171,77 @@ Mongous.connect!( hosts_or_uri = nil, **opts )
|
|
164
171
|
|
165
172
|
* Parameter:
|
166
173
|
** hosts_or_uri: ホスト配列または URI (default: ["localhost:21017"])
|
167
|
-
**
|
174
|
+
** options: オプション
|
168
175
|
*** file: データベース構成定義ファイルへのパス
|
169
176
|
*** mode: 実行モード (default: "development")
|
170
177
|
*** database: データベース名 (default: "test")
|
171
|
-
***
|
178
|
+
*** Mongo::Client.new のその他オプション引数
|
172
179
|
|
173
180
|
=== データベースに接続する
|
174
181
|
|
175
182
|
[source,ruby]
|
176
183
|
----
|
177
|
-
Mongous.connect( hosts_or_uri = nil, **
|
184
|
+
Mongous.connect( hosts_or_uri = nil, **options )
|
178
185
|
----
|
179
186
|
|
180
187
|
* Result:
|
181
|
-
** Mongo::Client
|
188
|
+
** Mongo::Client インスタンス.
|
189
|
+
|
190
|
+
=== コレクション操作クラスをデフォルト設定で定義する.
|
191
|
+
|
192
|
+
[source,ruby]
|
193
|
+
----
|
194
|
+
Mongous.attach!( *names )
|
195
|
+
----
|
196
|
+
|
197
|
+
* Result:
|
198
|
+
** nil.
|
182
199
|
|
183
|
-
|
200
|
+
* Parameter:
|
201
|
+
** names: コレクション名. (String または Symbol)
|
202
|
+
|
203
|
+
=== コレクション操作クラスにドキュメントの機能を取り入れる.
|
184
204
|
|
185
205
|
[source,ruby]
|
186
206
|
----
|
187
207
|
include Mongous::Document
|
188
208
|
----
|
189
209
|
|
210
|
+
=== 別のデータベースを割り当てる.
|
211
|
+
|
212
|
+
[source,ruby]
|
213
|
+
----
|
214
|
+
self.client=( client )
|
215
|
+
----
|
216
|
+
|
217
|
+
* Result:
|
218
|
+
** Mongo::Client インスタンス.
|
219
|
+
|
220
|
+
* Parameter:
|
221
|
+
** client: Mongo::Client インスタンス.
|
222
|
+
|
223
|
+
=== 別のコレクションを割り当てる.
|
224
|
+
|
225
|
+
[source,ruby]
|
226
|
+
----
|
227
|
+
self.collection_name=( collection_name )
|
228
|
+
----
|
229
|
+
|
230
|
+
* Result:
|
231
|
+
** Collection name 文字列.
|
232
|
+
|
233
|
+
* Parameter:
|
234
|
+
** collection_name: コレクション名.
|
235
|
+
|
190
236
|
=== ドキュメントの要素を定義する.
|
191
237
|
|
192
238
|
[source,ruby]
|
193
239
|
----
|
194
|
-
field(
|
240
|
+
field( symbol, *attrs, **items )
|
195
241
|
----
|
196
242
|
|
197
243
|
* Parameter:
|
198
|
-
**
|
244
|
+
** symbol: 項目名
|
199
245
|
** attrs: 項目属性
|
200
246
|
*** Class: 項目検証用 Class
|
201
247
|
*** Proc: 項目検証用 Proc
|
@@ -203,17 +249,20 @@ field( label, *attrs, &block )
|
|
203
249
|
*** Array: 項目検証用配列
|
204
250
|
*** Symbol: 特別な指示子
|
205
251
|
**** must: ナル値でも空文字列でもない
|
206
|
-
**
|
252
|
+
** items: 保存時の操作.
|
253
|
+
*** default: 未定義のときの値または Proc.
|
254
|
+
*** create: ドキュメントを新規保存するときの値または Proc.
|
255
|
+
*** update: ドキュメントを更新するときの値または Proc.
|
207
256
|
|
208
257
|
=== 保存や代入の前にドキュメントの要素を検証する.
|
209
258
|
|
210
259
|
[source,ruby]
|
211
260
|
----
|
212
|
-
verify( *
|
261
|
+
verify( *directives, &block )
|
213
262
|
----
|
214
263
|
|
215
264
|
* Parameter:
|
216
|
-
**
|
265
|
+
** directives: 条件シンボル
|
217
266
|
*** strict: 定義済み項目名であることを検証する.
|
218
267
|
** block: 各項目値を検証して真偽を返す内容を記述する.
|
219
268
|
|
@@ -221,12 +270,12 @@ verify( *syms, &block )
|
|
221
270
|
|
222
271
|
[source,ruby]
|
223
272
|
----
|
224
|
-
index( *
|
273
|
+
index( *symbols, **options )
|
225
274
|
----
|
226
275
|
|
227
276
|
* Parameter:
|
228
|
-
**
|
229
|
-
**
|
277
|
+
** symbols: 項目名
|
278
|
+
** options: Mongo::Collection#indexes() のオプション.
|
230
279
|
|
231
280
|
=== 項目値がナル値でも空文字列でもないことを検証する.
|
232
281
|
|
@@ -241,6 +290,129 @@ having?( label )
|
|
241
290
|
* Parameter:
|
242
291
|
** label: メソッド呼び出しする項目名.
|
243
292
|
|
293
|
+
=== 検索条件に名前をつける.
|
294
|
+
|
295
|
+
[source,ruby]
|
296
|
+
----
|
297
|
+
filter( symbol, filter_or_cond )
|
298
|
+
----
|
299
|
+
|
300
|
+
* Parameter:
|
301
|
+
** symbol: 項目名
|
302
|
+
** filter_or_cond: フィルタまたは検索条件
|
303
|
+
|
304
|
+
=== 検索条件.
|
305
|
+
|
306
|
+
[source,ruby]
|
307
|
+
----
|
308
|
+
コレクション操作クラス #where( filter = nil, **conditions )
|
309
|
+
----
|
310
|
+
|
311
|
+
* Result:
|
312
|
+
** Filter instance.
|
313
|
+
|
314
|
+
* Parameter:
|
315
|
+
** filter: 項目名またはフィルタインスタンス
|
316
|
+
** conditions: 検索条件
|
317
|
+
|
318
|
+
=== 否定検索条件.
|
319
|
+
|
320
|
+
[source,ruby]
|
321
|
+
----
|
322
|
+
コレクション操作クラス #not( filter = nil, **conditions )
|
323
|
+
----
|
324
|
+
|
325
|
+
* Result:
|
326
|
+
** Filter instance.
|
327
|
+
|
328
|
+
* Parameter:
|
329
|
+
** filter: 項目名またはフィルタインスタンス
|
330
|
+
** conditions: 検索条件
|
331
|
+
|
332
|
+
=== 論理積検索条件.
|
333
|
+
|
334
|
+
[source,ruby]
|
335
|
+
----
|
336
|
+
コレクション操作クラス #and( *filters )
|
337
|
+
----
|
338
|
+
|
339
|
+
* Result:
|
340
|
+
** Filter instance.
|
341
|
+
|
342
|
+
* Parameter:
|
343
|
+
** filters: 項目名またはフィルタインスタンス
|
344
|
+
|
345
|
+
=== 論理和検索条件.
|
346
|
+
|
347
|
+
[source,ruby]
|
348
|
+
----
|
349
|
+
コレクション操作クラス #or( *filters )
|
350
|
+
----
|
351
|
+
|
352
|
+
* Result:
|
353
|
+
** Filter instance.
|
354
|
+
|
355
|
+
* Parameter:
|
356
|
+
** filters: 項目名またはフィルタインスタンス
|
357
|
+
|
358
|
+
=== ドキュメントを保存.
|
359
|
+
|
360
|
+
[source,ruby]
|
361
|
+
----
|
362
|
+
ドキュメント操作オブジェクト #save
|
363
|
+
----
|
364
|
+
|
365
|
+
* Result:
|
366
|
+
** nil.
|
367
|
+
|
368
|
+
=== ドキュメントをHashに変換.
|
369
|
+
|
370
|
+
[source,ruby]
|
371
|
+
----
|
372
|
+
ドキュメント操作オブジェクト #to_hash
|
373
|
+
----
|
374
|
+
|
375
|
+
* Result:
|
376
|
+
** Hash object.
|
377
|
+
|
378
|
+
=== ドキュメントをJSONに変換.
|
379
|
+
|
380
|
+
[source,ruby]
|
381
|
+
----
|
382
|
+
ドキュメント操作オブジェクト #to_hash
|
383
|
+
----
|
384
|
+
|
385
|
+
* Result:
|
386
|
+
** JSON String.
|
387
|
+
|
388
|
+
=== ドキュメントの項目値を読む.
|
389
|
+
|
390
|
+
[source,ruby]
|
391
|
+
----
|
392
|
+
ドキュメント操作オブジェクト #[]( field_name )
|
393
|
+
ドキュメント操作オブジェクト #field_name
|
394
|
+
----
|
395
|
+
|
396
|
+
* Result:
|
397
|
+
** 項目値.
|
398
|
+
|
399
|
+
* Parameter:
|
400
|
+
** field_name: 項目名.
|
401
|
+
|
402
|
+
=== ドキュメントの項目値を書く.
|
403
|
+
|
404
|
+
[source,ruby]
|
405
|
+
----
|
406
|
+
ドキュメント操作オブジェクト #[]=( field_name, field_value )
|
407
|
+
ドキュメント操作オブジェクト #field_name = field_value
|
408
|
+
----
|
409
|
+
|
410
|
+
* Result:
|
411
|
+
** 項目値.
|
412
|
+
|
413
|
+
* Parameter:
|
414
|
+
** field_name: 項目名.
|
415
|
+
** field_value: 項目値.
|
244
416
|
|
245
417
|
== 貢献
|
246
418
|
|