forked from danielmewes/php-rql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
2437 lines (1584 loc) · 88.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html><head><link rel="stylesheet" type="text/css" href="api.css"></head><body><h1>PHP-RQL API reference</h1><h2 id="Index">Index</h2><ul><li><a href="#Accessing+ReQL">Accessing ReQL</a></li><li><a href="#Manipulating+databases">Manipulating databases</a></li><li><a href="#Manipulating+tables">Manipulating tables</a></li><li><a href="#Writing+data">Writing data</a></li><li><a href="#Selecting+data">Selecting data</a></li><li><a href="#Joins">Joins</a></li><li><a href="#Transformations">Transformations</a></li><li><a href="#Aggregation">Aggregation</a></li><li><a href="#Document+manipulation">Document manipulation</a></li><li><a href="#String+manipulation">String manipulation</a></li><li><a href="#Math+and+logic">Math and logic</a></li><li><a href="#Dates+and+times">Dates and times</a></li><li><a href="#Control+structures">Control structures</a></li><li><a href="#Geospatial+commands">Geospatial commands</a></li><li><a href="#Administration">Administration</a></li></ul><p><h2 id="Accessing+ReQL">Accessing ReQL</h2>
All ReQL queries begin from the top-level module.</p>
<h3>r</h3>
<pre class="syntax"><code class="syntax">r → r
</code></pre>
<p>The top-level ReQL namespace.</p>
<p><strong>Example:</strong> Set up your top-level namespace.</p>
<pre class="example"><code class="php">require_once('rdb/rdb.php');
</code></pre>
<h3>connect</h3>
<pre class="syntax"><code class="syntax">r\connect(host, port=28015[, db[, authKey[, timeout]]]) → connection
</code></pre>
<p>Create a new connection to the database server.</p>
<p><strong>Example:</strong> Open a connection using the default host and port, specifying the default database.</p>
<pre class="example"><code class="php">$conn = r\connect('localhost', 28015, "myDb")
</code></pre>
<h3>close</h3>
<pre class="syntax"><code class="syntax">conn->close(noreplyWait=true)
</code></pre>
<p>Close an open connection.</p>
<p><strong>Example:</strong> Close an open connection, waiting for noreply writes to finish.</p>
<pre class="example"><code class="php">$conn->close()
</code></pre>
<h3>reconnect</h3>
<pre class="syntax"><code class="syntax">conn->reconnect(noreplyWait=true)
</code></pre>
<p>Close and reopen a connection.</p>
<p><strong>Example:</strong> Cancel outstanding requests/queries that are no longer needed.</p>
<pre class="example"><code class="php">$conn->reconnect(false)
</code></pre>
<h3>useDb</h3>
<pre class="syntax"><code class="syntax">conn->useDb(dbName)
</code></pre>
<p>Change the default database on this connection.</p>
<p><strong>Example:</strong> Change the default database so that we don't need to
specify the database when referencing a table.</p>
<pre class="example"><code class="php">$conn->useDb('marvel')
r\table('heroes')->run($conn) // refers to r\db('marvel').table('heroes')
</code></pre>
<h3>run</h3>
<pre class="syntax"><code class="syntax">query->run(conn[, options]) → cursor
query->run(conn[, options]) → datum
</code></pre>
<p>Run a query on a connection.</p>
<p>Returns either a single result or a cursor, depending on the query.</p>
<p><strong>Example:</strong> Run a query on the connection <code>conn</code> and log each row in
the result to the console.</p>
<pre class="example"><code class="php">$cursor = r\table('marvel')->run($conn)
foreach ($cursor as $x) { print_r($x); }
</code></pre>
<h3>noreplyWait</h3>
<pre class="syntax"><code class="syntax">conn->noreplyWait()
</code></pre>
<p><code>noreplyWait</code> ensures that previous queries with the <code>noreply</code> flag have been processed
by the server. Note that this guarantee only applies to queries run on the given connection.</p>
<p><strong>Example:</strong> We have previously run queries with the <code>noreply</code> argument set to <code>true</code>. Now
wait until the server has processed them.</p>
<pre class="example"><code class="php">$conn->noreplyWait()
</code></pre>
<h3>server</h3>
<pre class="syntax"><code class="syntax">conn->server()
conn->server() → promise
</code></pre>
<p>Return the server name and server UUID being used by a connection.</p>
<p><strong>Example:</strong> Return the server name and UUID.</p>
<pre class="example"><code class="php">$conn->server();
// Result
array( "id" => "404bef53-4b2c-433f-9184-bc3f7bda4a15", "name" => "amadeus" )
</code></pre>
<h3>toArray</h3>
<pre class="syntax"><code class="syntax">cursor->toArray() → array
</code></pre>
<p>Retrieve all results and return them as an array.</p>
<p><strong>Example:</strong> For small result sets it may be more convenient to process them at once as
an array.</p>
<pre class="example"><code class="php">$fullResult = $cursor->toArray()
</code></pre>
<h3>close</h3>
<pre class="syntax"><code class="syntax">cursor->close()
</code></pre>
<p>Close a cursor. Closing a cursor cancels the corresponding query and frees the memory
associated with the open request.</p>
<p><strong>Example:</strong> Close a cursor.</p>
<pre class="example"><code class="php">$cursor->close()
</code></pre>
<h3>EventEmitter (cursor)</h3>
<pre class="syntax"><code class="syntax">cursor->addListener(event, listener)
cursor->on(event, listener)
cursor->once(event, listener)
cursor->removeListener(event, listener)
cursor->removeAllListeners([event])
cursor->setMaxListeners(n)
cursor->listeners(event)
cursor->emit(event, [arg1], [arg2], [...])
</code></pre>
<p>Cursors and feeds implement the same interface as Node's <a href="http://nodejs.org/api/events.html#events_class_events_eventemitter">EventEmitter</a>.</p>
<p></p>
<p><h2 id="Manipulating+databases">Manipulating databases</h2></p>
<h3>dbCreate</h3>
<pre class="syntax"><code class="syntax">r\dbCreate(dbName) → object
</code></pre>
<p>Create a database. A RethinkDB database is a collection of tables, similar to
relational databases.</p>
<p>If successful, the operation returns an object: <code>{created: 1}</code>. If a database with the
same name already exists the operation throws <code>ReqlRuntimeError</code>.</p>
<p>Note: that you can only use alphanumeric characters and underscores for the database name.</p>
<p><strong>Example:</strong> Create a database named 'superheroes'.</p>
<pre class="example"><code class="php">r\dbCreate('superheroes')->run($conn)
</code></pre>
<h3>dbDrop</h3>
<pre class="syntax"><code class="syntax">r\dbDrop(dbName) → object
</code></pre>
<p>Drop a database. The database, all its tables, and corresponding data will be deleted.</p>
<p>If successful, the operation returns the object <code>{dropped: 1}</code>. If the specified database
doesn't exist a <code>ReqlRuntimeError</code> is thrown.</p>
<p><strong>Example:</strong> Drop a database named 'superheroes'.</p>
<pre class="example"><code class="php">r\dbDrop('superheroes')->run($conn)
</code></pre>
<h3>dbList</h3>
<pre class="syntax"><code class="syntax">r\dbList() → array
</code></pre>
<p>List all database names in the system. The result is a list of strings.</p>
<p><strong>Example:</strong> List all databases.</p>
<pre class="example"><code class="php">r\dbList()->run($conn)
</code></pre>
<p></p>
<p><h2 id="Manipulating+tables">Manipulating tables</h2></p>
<h3>tableCreate</h3>
<pre class="syntax"><code class="syntax">db->tableCreate(tableName[, options]) → object
r\tableCreate(tableName[, options]) → object
</code></pre>
<p>Create a table. A RethinkDB table is a collection of JSON documents.</p>
<p><strong>Example:</strong> Create a table named 'dc_universe' with the default settings.</p>
<pre class="example"><code class="php">r\db('heroes')->tableCreate('dc_universe')->run($conn)
</code></pre>
<h3>tableDrop</h3>
<pre class="syntax"><code class="syntax">db->tableDrop(tableName) → object
</code></pre>
<p>Drop a table. The table and all its data will be deleted.</p>
<p><strong>Example:</strong> Drop a table named 'dc_universe'.</p>
<pre class="example"><code class="php">r\db('test')->tableDrop('dc_universe')->run($conn)
</code></pre>
<h3>tableList</h3>
<pre class="syntax"><code class="syntax">db->tableList() → array
</code></pre>
<p>List all table names in a database. The result is a list of strings.</p>
<p><strong>Example:</strong> List all tables of the 'test' database.</p>
<pre class="example"><code class="php">r\db('test')->tableList()->run($conn)
</code></pre>
<h3>indexCreate</h3>
<pre class="syntax"><code class="syntax">table->indexCreate(indexName[, indexFunction]) → object
table->indexCreateMulti(indexName[, indexFunction]) → object
table->indexCreateGeo(indexName[, indexFunction]) → object
table->indexCreateMultiGeo(indexName[, indexFunction]) → object
</code></pre>
<p>Create a new secondary index on a table.</p>
<p>The index can be either a regular index, a multi index, a geo index, or a multi geo index.</p>
<p><strong>Example:</strong> Create a simple index based on the field <code>postId</code>.</p>
<pre class="example"><code class="php">r\table('comments')->indexCreate('postId')->run($conn)
</code></pre>
<h3>indexDrop</h3>
<pre class="syntax"><code class="syntax">table->indexDrop(indexName) → object
</code></pre>
<p>Delete a previously created secondary index of this table.</p>
<p><strong>Example:</strong> Drop a secondary index named 'code_name'.</p>
<pre class="example"><code class="php">r\table('dc')->indexDrop('code_name')->run($conn)
</code></pre>
<h3>indexList</h3>
<pre class="syntax"><code class="syntax">table->indexList() → array
</code></pre>
<p>List all the secondary indexes of this table.</p>
<p><strong>Example:</strong> List the available secondary indexes for this table.</p>
<pre class="example"><code class="php">r\table('marvel')->indexList()->run($conn)
</code></pre>
<h3>indexRename</h3>
<pre class="syntax"><code class="syntax">table->indexRename(oldIndexName, newIndexName[, array('overwrite' => false)]) → object
</code></pre>
<p>Rename an existing secondary index on a table. If the optional argument <code>overwrite</code> is specified as <code>true</code>, a previously existing index with the new name will be deleted and the index will be renamed. If <code>overwrite</code> is <code>false</code> (the default) an error will be raised if the new index name already exists.</p>
<p><strong>Example:</strong> Rename an index on the comments table.</p>
<pre class="example"><code class="php">r\table('comments')->indexRename('postId', 'messageId')->run($conn)
</code></pre>
<h3>indexStatus</h3>
<pre class="syntax"><code class="syntax">table->indexStatus() → array
table->indexStatus(index) → array
table->indexStatus(array(index, ...)) → array
</code></pre>
<p>Get the status of the specified indexes on this table, or the status
of all indexes on this table if no indexes are specified.</p>
<p><strong>Example:</strong> Get the status of all the indexes on <code>test</code>:</p>
<pre class="example"><code class="php">r\table('test')->indexStatus()->run($conn)
</code></pre>
<p><strong>Example:</strong> Get the status of the <code>timestamp</code> index:</p>
<pre class="example"><code class="php">r\table('test')->indexStatus('timestamp')->run($conn)
</code></pre>
<h3>indexWait</h3>
<pre class="syntax"><code class="syntax">table->indexWait() → array
table->indexWait(index) → array
table->indexWait(array(index, ...)) → array
</code></pre>
<p>Wait for the specified indexes on this table to be ready, or for all
indexes on this table to be ready if no indexes are specified.</p>
<p><strong>Example:</strong> Wait for all indexes on the table <code>test</code> to be ready:</p>
<pre class="example"><code class="php">r\table('test')->indexWait()->run($conn)
</code></pre>
<p><strong>Example:</strong> Wait for the index <code>timestamp</code> to be ready:</p>
<pre class="example"><code class="php">r\table('test')->indexWait('timestamp')->run($conn)
</code></pre>
<h3>changes</h3>
<pre class="syntax"><code class="syntax">stream->changes([options]) → stream
singleSelection->changes([options]) → stream
</code></pre>
<p>Return a changefeed, an infinite stream of objects representing changes to a query. A changefeed may return changes to a table or an individual document (a "point" changefeed), and document transformation commands such as <code>filter</code> or <code>map</code> may be used before the <code>changes</code> command to affect the output.</p>
<p><strong>Example:</strong> Subscribe to the changes on a table.</p>
<pre class="example"><code class="php">$feed = r\table('games')->changes()->run($conn);
foreach ($feed as $change) {
print_r($change);
}
</code></pre>
<p></p>
<p><h2 id="Writing+data">Writing data</h2></p>
<h3>insert</h3>
<pre class="syntax"><code class="syntax">table->insert(object | array(object, ...)[, array('durability' => "hard", 'return_changes' => false, 'conflict' => "error")]) → object
</code></pre>
<p>Insert JSON documents into a table. Accepts a single JSON document or an array of
documents.</p>
<p><strong>Example:</strong> Insert a document into the table <code>posts</code>.</p>
<pre class="example"><code class="php">r\table("posts")->insert(array(
'id' => 1,
'title' => "Lorem ipsum",
'content' => "Dolor sit amet"
))->run($conn)
</code></pre>
<h3>update</h3>
<pre class="syntax"><code class="syntax">table->update(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object
selection->update(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object
singleSelection->update(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object
</code></pre>
<p>Update JSON documents in a table. Accepts a JSON document, a ReQL expression, or a
combination of the two. You can pass options like <code>return_changes</code> that will return the old
and new values of the row you have modified.</p>
<p><strong>Example:</strong> Update the status of the post with <code>id</code> of <code>1</code> to <code>published</code>.</p>
<pre class="example"><code class="php">r\table("posts")->get(1)->update(array('status' => "published"))->run($conn)
</code></pre>
<h3>replace</h3>
<pre class="syntax"><code class="syntax">table->replace(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object
selection->replace(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object
singleSelection->replace(object | function
[, array('durability' => "hard", 'return_changes' => false, 'non_atomic' => false)])
→ object</p>
</code></pre>
<p>Replace documents in a table. Accepts a JSON document or a ReQL expression, and replaces
the original document with the new one. The new document must have the same primary key
as the original document.</p>
<p><strong>Example:</strong> Replace the document with the primary key <code>1</code>.</p>
<pre class="example"><code class="php">r\table("posts")->get(1)->replace(array(
'id' => 1,
'title' => "Lorem ipsum",
'content' => "Aleas jacta est",
'status' => "draft"
))->run($conn)
</code></pre>
<h3>delete</h3>
<pre class="syntax"><code class="syntax">table->delete([array('durability' => "hard", 'return_changes' => false)])
→ object
selection->delete([array('durability' => "hard", 'return_changes' => false)])
→ object
singleSelection->delete([array('durability' => "hard", 'return_changes' => false)])
→ object
</code></pre>
<p>Delete one or more documents from a table.</p>
<p><strong>Example:</strong> Delete a single document from the table <code>comments</code>.</p>
<pre class="example"><code class="php">r\table("comments")->get("7eab9e63-73f1-4f33-8ce4-95cbea626f59")->delete()->run($conn)
</code></pre>
<h3>sync</h3>
<pre class="syntax"><code class="syntax">table->sync()
→ object
</code></pre>
<p><code>sync</code> ensures that writes on a given table are written to permanent storage. Queries
that specify soft durability (<code>{durability: 'soft'}</code>) do not give such guarantees, so
<code>sync</code> can be used to ensure the state of these queries. A call to <code>sync</code> does not return
until all previous writes to the table are persisted.</p>
<p><strong>Example:</strong> After having updated multiple heroes with soft durability, we now want to wait
until these changes are persisted.</p>
<pre class="example"><code class="php">r\table('marvel')->sync()->run($conn)
</code></pre>
<p></p>
<p><h2 id="Selecting+data">Selecting data</h2></p>
<h3>db</h3>
<pre class="syntax"><code class="syntax">r\db(dbName) → db
</code></pre>
<p>Reference a database.</p>
<p><strong>Example:</strong> Explicitly specify a database for a query.</p>
<pre class="example"><code class="php">r\db('heroes')->table('marvel')->run($conn)
</code></pre>
<h3>table</h3>
<pre class="syntax"><code class="syntax">db->table(name[, array('readMode' => 'single', 'identifierFormat' => 'name')]) → table
</code></pre>
<p>Select all documents in a table. This command can be chained with other commands to do
further processing on the data.</p>
<p><strong>Example:</strong> Return all documents in the table 'marvel' of the default database.</p>
<pre class="example"><code class="php">r\table('marvel')->run($conn)
</code></pre>
<h3>get</h3>
<pre class="syntax"><code class="syntax">table->get(key) → singleRowSelection
</code></pre>
<p>Get a document by primary key.</p>
<p>If no document exists with that primary key, <code>get</code> will return <code>null</code>.</p>
<p><strong>Example:</strong> Find a document by UUID.</p>
<pre class="example"><code class="php">r\table('posts')->get('a9849eef-7176-4411-935b-79a6e3c56a74')->run($conn)
</code></pre>
<h3>getAll</h3>
<pre class="syntax"><code class="syntax">table->getAll(key[, array('index' =>'id')]) → selection
table->getMultiple(array(key, ...)[, array('index' =>'id')]) → selection
</code></pre>
<p>Get all documents where the given value matches the value of the requested index.
Use <code>getMultiple</code> for retrieving documents under multiple keys at once.</p>
<p><strong>Example:</strong> Secondary index keys are not guaranteed to be unique so we cannot query via <a href="/api/javascript/get/">get</a> when using a secondary index.</p>
<pre class="example"><code class="php">r\table('marvel')->getAll('man_of_steel', array('index' =>'code_name'))->run($conn)
</code></pre>
<h3>between</h3>
<pre class="syntax"><code class="syntax">table->between(lowerKey, upperKey[, options]) → table_slice
table_slice->between(lowerKey, upperKey[, options]) → table_slice
</code></pre>
<p>Get all documents between two keys. Accepts three optional arguments: <code>index</code>,
<code>left_bound</code>, and <code>right_bound</code>. If <code>index</code> is set to the name of a secondary index,
<code>between</code> will return all documents where that index's value is in the specified range
(it uses the primary key by default). <code>left_bound</code> or <code>right_bound</code> may be set to <code>open</code>
or <code>closed</code> to indicate whether or not to include that endpoint of the range (by default,
<code>left_bound</code> is closed and <code>right_bound</code> is open).</p>
<p><strong>Example:</strong> Find all users with primary key >= 10 and < 20 (a normal half-open interval).</p>
<pre class="example"><code class="php">r\table('marvel')->between(10, 20)->run($conn)
</code></pre>
<h3>filter</h3>
<pre class="syntax"><code class="syntax">selection->filter(predicate_function[, array(default => false)]) → selection
stream->filter(predicate_function[, array(default => false)]) → stream
array->filter(predicate_function[, array(default => false)]) → array
</code></pre>
<p>Get all the documents for which the given predicate is true.</p>
<p><code>filter</code> can be called on a sequence, selection, or a field containing an array of
elements. The return type is the same as the type on which the function was called on.</p>
<p>The body of every filter is wrapped in an implicit <code>.default(false)</code>, which means that
if a non-existence errors is thrown (when you try to access a field that does not exist
in a document), RethinkDB will just ignore the document.
The <code>default</code> value can be changed by passing an object with a <code>default</code> field.
Setting this optional argument to <code>r.error()</code> will cause any non-existence errors to
return a <code>ReqlRuntimeError</code>.</p>
<p><strong>Example:</strong> Get all the users that are 30 years old.</p>
<pre class="example"><code class="php">r\table('users')->filter(array('age' => 30))->run($conn)
</code></pre>
<p></p>
<p><h2 id="Joins">Joins</h2>
These commands allow the combination of multiple sequences into a single sequence</p>
<h3>innerJoin</h3>
<pre class="syntax"><code class="syntax">sequence->innerJoin(otherSequence, predicate_function) → stream
array->innerJoin(otherSequence, predicate_function) → array
</code></pre>
<p>Returns an inner join of two sequences.</p>
<p><strong>Example:</strong> Return a list of all matchups between Marvel and DC heroes in which the DC hero could beat the Marvel hero in a fight.</p>
<pre class="example"><code class="php">r\table('marvel')->innerJoin(r\table('dc'), function($marvelRow, $dcRow) {
return $marvelRow('strength')->lt($dcRow('strength'));
})->zip()->run($conn)
</code></pre>
<h3>outerJoin</h3>
<pre class="syntax"><code class="syntax">sequence->outerJoin(otherSequence, predicate_function) → stream
array->outerJoin(otherSequence, predicate_function) → array
</code></pre>
<p>Returns a left outer join of two sequences.</p>
<p><strong>Example:</strong> Return a list of all Marvel heroes, paired with any DC heroes who could beat them in a fight.</p>
<pre class="example"><code class="php">r\table('marvel')->outerJoin(r\table('dc'), function($marvelRow, $dcRow) {
return $marvelRow('strength')->lt($dcRow('strength'));
})->run($conn)
</code></pre>
<h3>eqJoin</h3>
<pre class="syntax"><code class="syntax">sequence->eqJoin(leftField, rightTable[, array('index' =>'id')]) → sequence
sequence->eqJoin(predicate_function, rightTable[, array('index' =>'id')]) → sequence
</code></pre>
<p>Join tables using a field or function on the left-hand sequence matching primary keys or secondary indexes on the right-hand table. <code>eqJoin</code> is more efficient than other ReQL join types, and operates much faster. Documents in the result set consist of pairs of left-hand and right-hand documents, matched when the field on the left-hand side exists and is non-null and an entry with that field's value exists in the specified index on the right-hand side.</p>
<p><strong>Example:</strong> Match players with the games they've played against one another.</p>
<pre class="example"><code class="php">r\table('players')->eqJoin('gameId', r\table('games'))->run($conn)
</code></pre>
<h3>zip</h3>
<pre class="syntax"><code class="syntax">stream->zip() → stream
array->zip() → array
</code></pre>
<p>Used to 'zip' up the result of a join by merging the 'right' fields into 'left' fields of each member of the sequence.</p>
<p><strong>Example:</strong> 'zips up' the sequence by merging the left and right fields produced by a join.</p>
<pre class="example"><code class="php">r\table('marvel')->eqJoin('main_dc_collaborator', r\table('dc'))
->zip()->run($conn)
</code></pre>
<p></p>
<p><h2 id="Transformations">Transformations</h2>
These commands are used to transform data in a sequence.</p>
<h3>map</h3>
<pre class="syntax"><code class="syntax">sequence->map(mappingFunction) → stream
array->map(mappingFunction) → array
sequence1->mapMultiple(array(sequence2, ...), mappingFunction) → stream
array1->mapMultiple(array(array2, ...), mappingFunction) → array
r\mapMultiple(array(sequence1, sequence2, ...), mappingFunction) → stream
r\mapMultiple(array(array1, array2, ...), mappingFunction) → array
</code></pre>
<p>Transform each element of one or more sequences by applying a mapping function to them. For mapping over multiple sequences, <code>mapMultiple</code> must be used. <code>mapMultiple</code> will iterate for as many items as there are in the shortest sequence.</p>
<p><strong>Example:</strong> Return the first five squares.</p>
<pre class="example"><code class="php">$result = r\expr(array(1, 2, 3, 4, 5))->map(function ($val) {
return $val->mul($val);
})->run($conn);
// Result
array(1, 4, 9, 16, 25)
</code></pre>
<h3>withFields</h3>
<pre class="syntax"><code class="syntax">sequence->withFields(selector | array(selector1, selector2...)) → stream
array->withFields(selector | array(selector1, selector2...)) → array
</code></pre>
<p>Plucks one or more attributes from a sequence of objects, filtering out any objects in the sequence that do not have the specified fields. Functionally, this is identical to <code>hasFields</code> followed by <code>pluck</code> on a sequence.</p>
<p><strong>Example:</strong> Get a list of users and their posts, excluding any users who have not made any posts.</p>
<pre class="example"><code class="php">r\table('users')->withFields(array('id', 'username', 'posts'))->run($conn)
</code></pre>
<h3>concatMap</h3>
<pre class="syntax"><code class="syntax">stream->concatMap(function) → stream
array->concatMap(function) → array
</code></pre>
<p>Concatenate one or more elements into a single sequence using a mapping function.</p>
<p><strong>Example:</strong> Construct a sequence of all monsters defeated by Marvel heroes. The field "defeatedMonsters" is an array of one or more monster names.</p>
<pre class="example"><code class="php">r\table('marvel')->concatMap(function($hero) {
return $hero('defeatedMonsters');
})->run($conn)
</code></pre>
<h3>orderBy</h3>
<pre class="syntax"><code class="syntax">table->orderBy(keyOrFunction2 | array(keyOrFunction2, ...), array('index' => index_name)) → table_slice
selection->orderBy(keyOrFunction | array(keyOrFunction1, ,,,)) → selection<array>
sequence->orderBy(keyOrFunction | array(keyOrFunction1, ...)) → array
</code></pre>
<p>Sort the sequence by document values of the given key(s). To specify
the ordering, wrap the attribute with either <code>r\asc</code> or <code>r\desc</code>
(defaults to ascending).</p>
<p>Sorting without an index requires the server to hold the sequence in
memory, and is limited to 100,000 documents (or the setting of the <code>array_limit</code> option for run). Sorting with an index can
be done on arbitrarily large tables, or after a <code>between</code> command
using the same index.</p>
<p><strong>Example:</strong> Order all the posts using the index <code>date</code>.</p>
<pre class="example"><code class="php">r\table('posts')->orderBy(array('index' => 'date'))->run($conn)
</code></pre>
<p>The index must have been previously created with <a href="/api/javascript/index_create/">indexCreate</a>.</p>
<pre class="example"><code class="php">r\table('posts')->indexCreate('date')->run($conn)
</code></pre>
<p>You can also select a descending ordering:</p>
<pre class="example"><code class="php">r\table('posts')->orderBy(array('index' => r\desc('date')))->run($conn)
</code></pre>
<h3>skip</h3>
<pre class="syntax"><code class="syntax">sequence->skip(n) → stream
array->skip(n) → array
</code></pre>
<p>Skip a number of elements from the head of the sequence.</p>
<p><strong>Example:</strong> Here in conjunction with <code>orderBy</code> we choose to ignore the most successful heroes.</p>
<pre class="example"><code class="php">r\table('marvel')->orderBy('successMetric')->skip(10)->run($conn)
</code></pre>
<h3>limit</h3>
<pre class="syntax"><code class="syntax">sequence->limit(n) → stream
array->limit(n) → array
</code></pre>
<p>End the sequence after the given number of elements.</p>
<p><strong>Example:</strong> Only so many can fit in our Pantheon of heroes.</p>
<pre class="example"><code class="php">r\table('marvel')->orderBy('belovedness')->limit(10)->run($conn)
</code></pre>
<h3>slice</h3>
<pre class="syntax"><code class="syntax">selection->slice(startIndex[, endIndex, array('left_bound' =>'closed', 'right_bound' =>'open')]) → selection
stream->slice(startIndex[, endIndex, array('left_bound' =>'closed', 'right_bound' =>'open')]) → stream
array->slice(startIndex[, endIndex, array('left_bound' =>'closed', 'right_bound' =>'open')]) → array
binary->slice(startIndex[, endIndex, array('left_bound' =>'closed', 'right_bound' =>'open')]) → binary
</code></pre>
<p>Return the elements of a sequence within the specified range.</p>
<p><strong>Example:</strong> Return the fourth, fifth and sixth youngest players. (The youngest player is at index 0, so those are elements 3–5.)</p>
<pre class="example"><code class="php">r\table('players')->orderBy(array('index' => 'age'))->slice(3,6)->run($conn)
</code></pre>
<h3>nth</h3>
<pre class="syntax"><code class="syntax">sequence->nth(index) → object
selection->nth(index) → selection<object>
</code></pre>
<p>Get the <em>nth</em> element of a sequence, counting from zero. If the argument is negative, count from the last element.</p>
<p><strong>Example:</strong> Select the second element in the array.</p>
<pre class="example"><code class="php">r\expr(array(1,2,3))->nth(1)->run($conn)
</code></pre>
<h3>offsetsOf</h3>
<pre class="syntax"><code class="syntax">sequence->offsetsOf(datum | predicate_function) → array
</code></pre>
<p>Get the indexes of an element in a sequence. If the argument is a predicate, get the indexes of all elements matching it.</p>
<p><strong>Example:</strong> Find the position of the letter 'c'.</p>
<pre class="example"><code class="php">r\expr(array('a','b','c'))->offsetsOf('c')->run($conn)
</code></pre>
<h3>isEmpty</h3>
<pre class="syntax"><code class="syntax">sequence->isEmpty() → bool
</code></pre>
<p>Test if a sequence is empty.</p>
<p><strong>Example:</strong> Are there any documents in the marvel table?</p>
<pre class="example"><code class="php">r\table('marvel')->isEmpty()->run($conn)
</code></pre>
<h3>union</h3>
<pre class="syntax"><code class="syntax">stream->union(sequence) → stream
array->union(sequence) → array
</code></pre>
<p>Merge two sequences. (Note that ordering is not guaranteed by <code>union</code>.)</p>
<p><strong>Example:</strong> Construct a stream of all heroes.</p>
<pre class="example"><code class="php">r\table('marvel')->union(r\table('dc'))->run($conn);
</code></pre>
<h3>sample</h3>
<pre class="syntax"><code class="syntax">sequence->sample(number) → selection
stream->sample(number) → array
array->sample(number) → array
</code></pre>
<p>Select a given number of elements from a sequence with uniform random distribution. Selection is done without replacement.</p>
<p><strong>Example:</strong> Select 3 random heroes.</p>
<pre class="example"><code class="php">r\table('marvel')->sample(3)->run($conn)
</code></pre>
<p></p>
<p><h2 id="Aggregation">Aggregation</h2>
These commands are used to compute smaller values from large sequences.</p>
<h3>group</h3>
<pre class="syntax"><code class="syntax">sequence->group(fieldOrFunction[, array('index' => "indexName", 'multi' => false)]) → grouped_stream
</code></pre>
<p>Takes a stream and partitions it into multiple groups based on the
fields or functions provided. Commands chained after <code>group</code> will be
called on each of these grouped sub-streams, producing grouped data.</p>
<p><strong>Example:</strong> What is each player's best game?</p>
<pre class="example"><code class="php">r\table('games')->group('player')->max('points')->run($conn)
</code></pre>
<h3>ungroup</h3>
<pre class="syntax"><code class="syntax">grouped_stream->ungroup() → array
grouped_data->ungroup() → array
</code></pre>
<p>Takes a grouped stream or grouped data and turns it into an array of
objects representing the groups. Any commands chained after <code>ungroup</code>
will operate on this array, rather than operating on each group
individually. This is useful if you want to e.g. order the groups by
the value of their reduction.</p>
<p><strong>Example:</strong> What is the maximum number of points scored by each
player, with the highest scorers first?</p>
<pre class="example"><code class="php">r\table('games')
->group('player')->max('points')->getField('points')
->ungroup()->orderBy(r\desc('reduction'))->run($conn)
</code></pre>
<h3>reduce</h3>
<pre class="syntax"><code class="syntax">sequence->reduce(function) → value
</code></pre>
<p>Produce a single value from a sequence through repeated application of a reduction
function.</p>
<p><strong>Example:</strong> Return the number of documents in the table `posts.</p>
<pre class="example"><code class="php">r\table("posts")->map(function($doc) {
return 1;
})->reduce(function($left, $right) {
return $left->add($right);
})->run($conn);
</code></pre>
<h3>count</h3>
<pre class="syntax"><code class="syntax">sequence->count([value | predicate_function]) → number
binary->count() → number
</code></pre>
<p>Count the number of elements in the sequence. With a single argument, count the number
of elements equal to it. If the argument is a function, it is equivalent to calling
filter before count.</p>
<p><strong>Example:</strong> Just how many super heroes are there?</p>
<pre class="example"><code class="php">r\table('marvel')->count()->add(r\table('dc')->count())->run($conn)
</code></pre>
<h3>sum</h3>
<pre class="syntax"><code class="syntax">sequence->sum([field | function]) → number
</code></pre>
<p>Sums all the elements of a sequence. If called with a field name,
sums all the values of that field in the sequence, skipping elements
of the sequence that lack that field. If called with a function,
calls that function on every element of the sequence and sums the
results, skipping elements of the sequence where that function returns
<code>null</code> or a non-existence error.</p>
<p><strong>Example:</strong> What's 3 + 5 + 7?</p>
<pre class="example"><code class="php">r\expr(array(3, 5, 7))->sum()->run($conn)
</code></pre>
<h3>avg</h3>
<pre class="syntax"><code class="syntax">sequence->avg([field | function]) → number
</code></pre>
<p>Averages all the elements of a sequence. If called with a field name,
averages all the values of that field in the sequence, skipping
elements of the sequence that lack that field. If called with a
function, calls that function on every element of the sequence and
averages the results, skipping elements of the sequence where that
function returns <code>null</code> or a non-existence error.</p>
<p><strong>Example:</strong> What's the average of 3, 5, and 7?</p>
<pre class="example"><code class="php">r\expr(array(3, 5, 7))->avg()->run($conn)
</code></pre>
<h3>min</h3>
<pre class="syntax"><code class="syntax">sequence->min(field | function) → element
sequence->min(array('index' => <indexname>)) → element
</code></pre>
<p>Finds the minimum element of a sequence.</p>
<p><strong>Example:</strong> Return the minimum value in the list <code>[3, 5, 7]</code>.</p>
<pre class="example"><code class="php">r\expr(array(3, 5, 7))->min()->run($conn);
</code></pre>
<h3>max</h3>
<pre class="syntax"><code class="syntax">sequence->max(field | function) → element
sequence->max(array('index' => <indexname>)) → element
</code></pre>
<p>Finds the maximum element of a sequence.</p>
<p><strong>Example:</strong> Return the maximum value in the list <code>[3, 5, 7]</code>.</p>
<pre class="example"><code class="php">r\expr(array(3, 5, 7))->max()->run($conn);
</code></pre>
<h3>distinct</h3>
<pre class="syntax"><code class="syntax">sequence->distinct() → array
table->distinct([array('index' => <indexname>)]) → stream
</code></pre>
<p>Remove duplicate elements from the sequence.</p>
<p><strong>Example:</strong> Which unique villains have been vanquished by marvel heroes?</p>
<pre class="example"><code class="php">r\table('marvel')->concatMap(function($hero) {
return $hero('villainList');
})->distinct()->run($conn)
</code></pre>
<h3>contains</h3>
<pre class="syntax"><code class="syntax">sequence->contains(value | function) → bool
</code></pre>
<p>Returns whether or not a sequence contains the specified value, or if functions are
provided instead, returns whether or not a sequence contains values matching the
specified function.</p>
<p><strong>Example:</strong> Has Iron Man ever fought Superman?</p>
<pre class="example"><code class="php">r\table('marvel')->get('ironman')->getField('opponents')->contains('superman')->run($conn)
</code></pre>
<p></p>
<p><h2 id="Document+manipulation">Document manipulation</h2></p>
<h3>row</h3>
<pre class="syntax"><code class="syntax">r\row([field]) → value
</code></pre>
<p>Returns the currently visited document.</p>
<p><strong>Example:</strong> Get all users whose age is greater than 5.</p>
<pre class="example"><code class="php">r\table('users')->filter(r\row('age')->gt(5))->run($conn)
</code></pre>
<h3>pluck</h3>
<pre class="syntax"><code class="syntax">sequence->pluck(selector | array(selector1, selector2...)) → stream
array->pluck(selector | array(selector1, selector2...)) → array
object->pluck(selector | array(selector1, selector2...)) → object
singleSelection->pluck(selector | array(selector1, selector2...)) → object
</code></pre>
<p>Plucks out one or more attributes from either an object or a sequence of objects
(projection).</p>
<p><strong>Example:</strong> We just need information about IronMan's reactor and not the rest of the
document.</p>
<pre class="example"><code class="php">r\table('marvel')->get('IronMan')->pluck(array('reactorState', 'reactorPower'))->run($conn)
</code></pre>