|
69 | 69 | Scroll by `:position` and save a cursor to the last item. |
70 | 70 |
|
71 | 71 | ```ruby |
72 | | -saved_cursor = nil |
73 | | -Feed::Item.desc(:position).limit(5).scroll do |record, next_cursor| |
| 72 | +saved_iterator = nil |
| 73 | + |
| 74 | +Feed::Item.desc(:position).limit(5).scroll do |record, iterator| |
74 | 75 | # each record, one-by-one |
75 | | - saved_cursor = next_cursor |
| 76 | + saved_iterator = iterator |
76 | 77 | end |
77 | 78 | ``` |
78 | 79 |
|
79 | | -Resume iterating using the previously saved cursor. |
| 80 | +Resume iterating using saved cursor and save the cursor to go backward. |
| 81 | + |
| 82 | +```ruby |
| 83 | +Feed::Item.desc(:position).limit(5).scroll(saved_iterator.next_cursor) do |record, iterator| |
| 84 | + # each record, one-by-one |
| 85 | + saved_iterator = iterator |
| 86 | +end |
| 87 | +``` |
| 88 | + |
| 89 | +Loop over the first records again. |
80 | 90 |
|
81 | 91 | ```ruby |
82 | | -Feed::Item.desc(:position).limit(5).scroll(saved_cursor) do |record, next_cursor| |
| 92 | +Feed::Item.desc(:position).limit(5).scroll(saved_iterator.previous_cursor) do |record, iterator| |
83 | 93 | # each record, one-by-one |
84 | | - saved_cursor = next_cursor |
| 94 | + saved_iterator = iterator |
85 | 95 | end |
86 | 96 | ``` |
87 | 97 |
|
88 | 98 | The iteration finishes when no more records are available. You can also finish iterating over the remaining records by omitting the query limit. |
89 | 99 |
|
90 | 100 | ```ruby |
91 | | -Feed::Item.desc(:position).scroll(saved_cursor) do |record, next_cursor| |
| 101 | +Feed::Item.desc(:position).limit(5).scroll(saved_iterator.next_cursor) do |record, iterator| |
92 | 102 | # each record, one-by-one |
| 103 | + saved_iterator = iterator |
93 | 104 | end |
94 | 105 | ``` |
95 | 106 |
|
|
98 | 109 | Scroll a `Mongo::Collection::View` and save a cursor to the last item. You must also supply a `field_type` of the sort criteria. |
99 | 110 |
|
100 | 111 | ```ruby |
101 | | -saved_cursor = nil |
102 | | -client[:feed_items].find.sort(position: -1).limit(5).scroll(nil, { field_type: DateTime }) do |record, next_cursor| |
| 112 | +saved_iterator = nil |
| 113 | +client[:feed_items].find.sort(position: -1).limit(5).scroll(nil, { field_type: DateTime }) do |record, iterator| |
103 | 114 | # each record, one-by-one |
104 | | - saved_cursor = next_cursor |
| 115 | + saved_iterator = iterator |
105 | 116 | end |
106 | 117 | ``` |
107 | 118 |
|
108 | 119 | Resume iterating using the previously saved cursor. |
109 | 120 |
|
110 | 121 | ```ruby |
111 | | -session[:feed_items].find.sort(position: -1).limit(5).scroll(saved_cursor, { field_type: DateTime }) do |record, next_cursor| |
| 122 | +session[:feed_items].find.sort(position: -1).limit(5).scroll(saved_iterator.next_cursor, { field_type: DateTime }) do |record, iterator| |
112 | 123 | # each record, one-by-one |
113 | | - saved_cursor = next_cursor |
| 124 | + saved_iterator = iterator |
114 | 125 | end |
115 | 126 | ``` |
116 | 127 |
|
@@ -179,15 +190,15 @@ Feed::Item.desc(:created_at).scroll(cursor) # Raises a Mongoid::Scroll::Errors:: |
179 | 190 |
|
180 | 191 | ### Standard Cursor |
181 | 192 |
|
182 | | -The `Mongoid::Scroll::Cursor` encodes a value and a tiebreak ID separated by `:`, and does not include other options, such as scroll direction. Take extra care not to pass a cursor into a scroll with different options. |
| 193 | +The `Mongoid::Scroll::Cursor` encodes a value and a tiebreak ID separated by `:`, and does not include other options, such as scroll direction. Take extra care not to pass a cursor into a scroll with different options. |
183 | 194 |
|
184 | 195 | ### Base64 Encoded Cursor |
185 | 196 |
|
186 | 197 | The `Mongoid::Scroll::Base64EncodedCursor` can be used instead of `Mongoid::Scroll::Cursor` to generate a base64-encoded string (using RFC 4648) containing all the information needed to rebuild a cursor. |
187 | 198 |
|
188 | 199 | ```ruby |
189 | | -Feed::Item.desc(:position).limit(5).scroll(Mongoid::Scroll::Base64EncodedCursor) do |record, next_cursor| |
190 | | - # next_cursor is of type Mongoid::Scroll::Base64EncodedCursor |
| 200 | +Feed::Item.desc(:position).limit(5).scroll(Mongoid::Scroll::Base64EncodedCursor) do |record, iterator| |
| 201 | + # iterator.next_cursor is of type Mongoid::Scroll::Base64EncodedCursor |
191 | 202 | end |
192 | 203 | ``` |
193 | 204 |
|
|
0 commit comments