|
| 1 | +# start-simple-field-query |
| 2 | +Band.where(name: 'Depeche Mode') |
| 3 | +Band.where('name' => 'Depeche Mode') |
| 4 | +# end-simple-field-query |
| 5 | + |
| 6 | +# start-query-api-query |
| 7 | +Band.where(founded: {'$gt' => 1980}) |
| 8 | +Band.where('founded' => {'$gt' => 1980}) |
| 9 | +# end-query-api-query |
| 10 | + |
| 11 | +# start-symbol-query |
| 12 | +Band.where(:founded.gt => 1980) |
| 13 | +# end-symbol-query |
| 14 | + |
| 15 | +# start-defined-field-query |
| 16 | +Band.where(founded: '2020') |
| 17 | +# end-defined-field-query |
| 18 | + |
| 19 | +# start-raw-field-query |
| 20 | +Band.where(founded: Mongoid::RawValue('2020')) |
| 21 | +# end-raw-field-query |
| 22 | + |
| 23 | +# start-id-field-query |
| 24 | +Band.where(id: '5ebdeddfe1b83265a376a760') |
| 25 | +Band.where(_id: '5ebdeddfe1b83265a376a760') |
| 26 | +# end-id-field-query |
| 27 | + |
| 28 | +# start-embedded-query |
| 29 | +Band.where('manager.name' => 'Smith') |
| 30 | +# end-embedded-query |
| 31 | + |
| 32 | +# start-embedded-ne-query |
| 33 | +Band.where(:'manager.name'.ne => 'Smith') |
| 34 | +# end-embedded-ne-query |
| 35 | + |
| 36 | +# start-logical-ops |
| 37 | +# Uses "and" to combine criteria |
| 38 | +Band.where(label: 'Trust in Trance').and(name: 'Astral Projection') |
| 39 | + |
| 40 | +# Uses "or" to specify criteria |
| 41 | +Band.where(label: 'Trust in Trance').or(Band.where(name: 'Astral Projection')) |
| 42 | + |
| 43 | +# Uses "not" to specify criteria |
| 44 | +Band.not(label: 'Trust in Trance', name: 'Astral Projection') |
| 45 | + |
| 46 | +# Uses "not" without arguments |
| 47 | +Band.not.where(label: 'Trust in Trance', name: 'Astral Projection') |
| 48 | +# end-logical-ops |
| 49 | + |
| 50 | +# start-logical-and-ops |
| 51 | +# Conditions passed to separate "and" calls |
| 52 | +Band.and(name: 'Sun Kil Moon').and(member_count: 2) |
| 53 | + |
| 54 | +# Multiple conditions in the same "and" call |
| 55 | +Band.and({name: 'Sun Kil Moon'}, {member_count: 2}) |
| 56 | + |
| 57 | +# Multiple conditions in an array - Deprecated |
| 58 | +Band.and([{name: 'Sun Kil Moon'}, {member_count: 2}]) |
| 59 | + |
| 60 | +# Condition in "where" and a scope |
| 61 | +Band.where(name: 'Sun Kil Moon').and(Band.where(member_count: 2)) |
| 62 | + |
| 63 | +# Condition in "and" and a scope |
| 64 | +Band.and({name: 'Sun Kil Moon'}, Band.where(member_count: 2)) |
| 65 | + |
| 66 | +# Scope as an array element, nested arrays - Deprecated |
| 67 | +Band.and([Band.where(name: 'Sun Kil Moon'), [{member_count: 2}]]) |
| 68 | +# end-logical-and-ops |
| 69 | + |
| 70 | +# start-logical-combination-ops |
| 71 | +# Combines as "and" |
| 72 | +Band.where(name: 'Swans').where(name: 'Feist') |
| 73 | + |
| 74 | +# Combines as "or" |
| 75 | +Band.where(name: 'Swans').or(name: 'Feist') |
| 76 | +# end-logical-combination-ops |
| 77 | + |
| 78 | +# start-logical-combination-ops-2 |
| 79 | +# "or" applies to the first condition, and the second is combined |
| 80 | +# as "and" |
| 81 | +Band.or(name: 'Sun').where(label: 'Trust') |
| 82 | + |
| 83 | +# Same as previous example - "where" and "and" are aliases |
| 84 | +Band.or(name: 'Sun').and(label: 'Trust') |
| 85 | + |
| 86 | +# Same operator can be stacked any number of times |
| 87 | +Band.or(name: 'Sun').or(label: 'Trust') |
| 88 | + |
| 89 | +# The last label condition is added to the top level as "and" |
| 90 | +Band.where(name: 'Sun').or(label: 'Trust').where(label: 'Feist') |
| 91 | +# Interpreted query: |
| 92 | +# {"$or"=>[{"name"=>"Sun"}, {"label"=>"Trust"}], "label"=>"Feist"} |
| 93 | +# end-logical-combination-ops-2 |
| 94 | + |
| 95 | +# start-not-logical |
| 96 | +# "not" negates "where" |
| 97 | +Band.not.where(name: 'Best') |
| 98 | + |
| 99 | +# The second "where" is added as "$and" |
| 100 | +Band.not.where(name: 'Best').where(label: /Records/) |
| 101 | + |
| 102 | +# "not" negates its argument |
| 103 | +Band.not(name: 'Best') |
| 104 | +# end-not-logical |
| 105 | + |
| 106 | +# start-not-logical-note |
| 107 | +# String negation - uses "$ne" |
| 108 | +Band.not.where(name: 'Best') |
| 109 | + |
| 110 | +# Regex negation - uses "$not" |
| 111 | +Band.not.where(name: /Best/) |
| 112 | +# end-not-logical-note |
| 113 | + |
| 114 | +# start-not-behavior |
| 115 | +# Simple condition |
| 116 | +Band.not(name: /Best/) |
| 117 | + |
| 118 | +# Complex conditions |
| 119 | +Band.where(name: /Best/).not(name: 'Astral Projection') |
| 120 | + |
| 121 | +# Symbol operator syntax |
| 122 | +Band.not(:name.ne => 'Astral Projection') |
| 123 | +# end-not-behavior |
| 124 | + |
| 125 | +# start-incremental-1 |
| 126 | +Band.in(name: ['a']).in(name: ['b']) |
| 127 | +# Interpreted query: |
| 128 | +# {"name"=>{"$in"=>["a"]}, "$and"=>[{"name"=>{"$in"=>["b"]}}]} |
| 129 | +# end-incremental-1 |
| 130 | + |
| 131 | +# start-in-merge |
| 132 | +Band.in(name: ['a']).override.in(name: ['b']) |
| 133 | +# Interpreted query: |
| 134 | +# {"name"=>{"$in"=>["b"]}} |
| 135 | + |
| 136 | +Band.in(name: ['a', 'b']).intersect.in(name: ['b', 'c']) |
| 137 | +# Interpreted query: |
| 138 | +# {"name"=>{"$in"=>["b"]}} |
| 139 | + |
| 140 | +Band.in(name: ['a']).union.in(name: ['b']) |
| 141 | +# Interpreted query: |
| 142 | +# {"name"=>{"$in"=>["a", "b"]}} |
| 143 | +# end-in-merge |
| 144 | + |
| 145 | +# start-merge-reset |
| 146 | +Band.in(name: ['a']).union.ne(name: 'c').in(name: ['b']) |
| 147 | +# Interpreted query: |
| 148 | +# {"name"=>{"$in"=>["a"], "$ne"=>"c"}, "$and"=>[{"name"=>{"$in"=>["b"]}}]} |
| 149 | +# end-merge-reset |
| 150 | + |
| 151 | +# start-merge-where |
| 152 | +Band.in(name: ['a']).union.where(name: {'$in' => 'b'}) |
| 153 | +# Interpreted query: |
| 154 | +# {"foo"=>{"$in"=>["a"]}, "$and"=>[{"foo"=>{"$in"=>"b"}}]} |
| 155 | +# end-merge-where |
| 156 | + |
| 157 | +# start-range-query |
| 158 | +Band.in(year: 1950..1960) |
| 159 | +# Interpreted query: |
| 160 | +# {"year"=>{"$in"=>[1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960]}} |
| 161 | +# end-range-query |
| 162 | + |
| 163 | + |
0 commit comments