gopkg.in/rethinkdb/rethinkdb-go.v6@v6.2.2/internal/integration/tests/example_query_transformation_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	r "gopkg.in/rethinkdb/rethinkdb-go.v6"
     6  )
     7  
     8  // Return the first five squares.
     9  func ExampleTerm_Map() {
    10  	cur, err := r.Expr([]int{1, 2, 3, 4, 5}).Map(func(val r.Term) r.Term {
    11  		return val.Mul(val)
    12  	}).Run(session)
    13  	if err != nil {
    14  		fmt.Print(err)
    15  		return
    16  	}
    17  
    18  	var res []int
    19  	err = cur.All(&res)
    20  	if err != nil {
    21  		fmt.Print(err)
    22  		return
    23  	}
    24  
    25  	fmt.Print(res)
    26  
    27  	// Output:
    28  	// [1 4 9 16 25]
    29  }
    30  
    31  // Sum the elements of three sequences.
    32  func ExampleMap_multipleSequences() {
    33  	var sequence1 = []int{100, 200, 300, 400}
    34  	var sequence2 = []int{10, 20, 30, 40}
    35  	var sequence3 = []int{1, 2, 3, 4}
    36  
    37  	cur, err := r.Map(sequence1, sequence2, sequence3, func(val1, val2, val3 r.Term) r.Term {
    38  		return val1.Add(val2).Add(val3)
    39  	}).Run(session)
    40  	if err != nil {
    41  		fmt.Print(err)
    42  		return
    43  	}
    44  
    45  	var res []int
    46  	err = cur.All(&res)
    47  	if err != nil {
    48  		fmt.Print(err)
    49  		return
    50  	}
    51  
    52  	fmt.Print(res)
    53  
    54  	// Output:
    55  	// [111 222 333 444]
    56  }
    57  
    58  // Order all the posts using the index date.
    59  func ExampleTerm_OrderBy_index() {
    60  	cur, err := r.DB("examples").Table("posts").OrderBy(r.OrderByOpts{
    61  		Index: "date",
    62  	}).Run(session)
    63  	if err != nil {
    64  		fmt.Print(err)
    65  		return
    66  	}
    67  
    68  	var res []interface{}
    69  	err = cur.All(&res)
    70  	if err != nil {
    71  		fmt.Print(err)
    72  		return
    73  	}
    74  
    75  	fmt.Print(res)
    76  }
    77  
    78  // Order all the posts using the index date in descending order.
    79  func ExampleTerm_OrderBy_indexDesc() {
    80  	cur, err := r.DB("examples").Table("posts").OrderBy(r.OrderByOpts{
    81  		Index: r.Desc("date"),
    82  	}).Run(session)
    83  	if err != nil {
    84  		fmt.Print(err)
    85  		return
    86  	}
    87  
    88  	var res []interface{}
    89  	err = cur.All(&res)
    90  	if err != nil {
    91  		fmt.Print(err)
    92  		return
    93  	}
    94  
    95  	fmt.Print(res)
    96  }
    97  
    98  // You can efficiently order using multiple fields by using a compound index.
    99  // For example order by date and title.
   100  func ExampleTerm_OrderBy_compound() {
   101  	cur, err := r.DB("examples").Table("posts").OrderBy(r.OrderByOpts{
   102  		Index: r.Desc("dateAndTitle"),
   103  	}).Run(session)
   104  	if err != nil {
   105  		fmt.Print(err)
   106  		return
   107  	}
   108  
   109  	var res []interface{}
   110  	err = cur.All(&res)
   111  	if err != nil {
   112  		fmt.Print(err)
   113  		return
   114  	}
   115  
   116  	fmt.Print(res)
   117  }
   118  
   119  // If you have a sequence with fewer documents than the arrayLimit, you can order
   120  // it by multiple fields without an index.
   121  func ExampleTerm_OrderBy_multiple() {
   122  	cur, err := r.DB("examples").Table("posts").OrderBy(
   123  		"title",
   124  		r.OrderByOpts{Index: r.Desc("date")},
   125  	).Run(session)
   126  	if err != nil {
   127  		fmt.Print(err)
   128  		return
   129  	}
   130  
   131  	var res []interface{}
   132  	err = cur.All(&res)
   133  	if err != nil {
   134  		fmt.Print(err)
   135  		return
   136  	}
   137  
   138  	fmt.Print(res)
   139  }
   140  
   141  // Notice that an index ordering always has highest precedence. The following
   142  // query orders posts by date, and if multiple posts were published on the same
   143  // date, they will be ordered by title.
   144  func ExampleTerm_OrderBy_multipleWithIndex() {
   145  	cur, err := r.DB("examples").Table("posts").OrderBy(
   146  		"title",
   147  		r.OrderByOpts{Index: r.Desc("date")},
   148  	).Run(session)
   149  	if err != nil {
   150  		fmt.Print(err)
   151  		return
   152  	}
   153  
   154  	var res []interface{}
   155  	err = cur.All(&res)
   156  	if err != nil {
   157  		fmt.Print(err)
   158  		return
   159  	}
   160  
   161  	fmt.Print(res)
   162  }