Class: XGen::Mongo::Driver::Collection
- Inherits:
-
Object
- Object
- XGen::Mongo::Driver::Collection
- Defined in:
- lib/mongo/collection.rb
Overview
A named collection of records in a database.
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#hint ⇒ Object
Returns the value of attribute hint.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all records.
-
#count(selector = {}) ⇒ Object
Return the number of records that match
selector
. -
#create_index(name, *fields) ⇒ Object
Create a new index named
index_name
. -
#drop ⇒ Object
Drop the entire collection.
-
#drop_index(name) ⇒ Object
Drop index
name
. -
#drop_indexes ⇒ Object
Drop all indexes.
-
#find(selector = {}, options = {}) ⇒ Object
Return records that match a
selector
hash. -
#find_first(selector = {}, options = {}) ⇒ Object
Find the first record that matches
selector
. -
#index_information ⇒ Object
Return an array of hashes, one for each index.
-
#initialize(db, name) ⇒ Collection
constructor
A new instance of Collection.
-
#insert(*objects) ⇒ Object
(also: #<<)
Insert
objects
, which are hashes. -
#modify(selector, modifier_obj) ⇒ Object
Update records that match
selector
by applyingobj
as an update. -
#options ⇒ Object
Return a hash containing options that apply to this collection.
-
#remove(selector = {}) ⇒ Object
Remove the records that match
selector
. -
#replace(selector, obj) ⇒ Object
Update records that match
selector
by applyingobj
as an update. -
#repsert(selector, obj) ⇒ Object
Update records that match
selector
by applyingobj
as an update.
Constructor Details
#initialize(db, name) ⇒ Collection
Returns a new instance of Collection.
28 29 30 31 |
# File 'lib/mongo/collection.rb', line 28 def initialize(db, name) @db, @name = db, name @hint = nil end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
26 27 28 |
# File 'lib/mongo/collection.rb', line 26 def db @db end |
#hint ⇒ Object
Returns the value of attribute hint.
26 27 28 |
# File 'lib/mongo/collection.rb', line 26 def hint @hint end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/mongo/collection.rb', line 26 def name @name end |
Instance Method Details
#clear ⇒ Object
Remove all records.
93 94 95 |
# File 'lib/mongo/collection.rb', line 93 def clear remove({}) end |
#count(selector = {}) ⇒ Object
Return the number of records that match selector
. If selector
is nil
or an empty hash, returns the count of all records.
160 161 162 |
# File 'lib/mongo/collection.rb', line 160 def count(selector={}) @db.count(@name, selector || {}) end |
#create_index(name, *fields) ⇒ Object
Create a new index named index_name
. fields
should be an array of field names.
118 119 120 121 |
# File 'lib/mongo/collection.rb', line 118 def create_index(name, *fields) fields = *fields if fields.kind_of?(Array) && fields.length == 1 @db.create_index(@name, name, fields) end |
#drop ⇒ Object
Drop the entire collection. USE WITH CAUTION.
135 136 137 |
# File 'lib/mongo/collection.rb', line 135 def drop @db.drop_collection(@name) end |
#drop_index(name) ⇒ Object
Drop index name
.
124 125 126 |
# File 'lib/mongo/collection.rb', line 124 def drop_index(name) @db.drop_index(@name, name) end |
#drop_indexes ⇒ Object
Drop all indexes.
129 130 131 132 |
# File 'lib/mongo/collection.rb', line 129 def drop_indexes # just need to call drop indexes with no args; will drop them all @db.drop_index(@name, '*') end |
#find(selector = {}, options = {}) ⇒ Object
Return records that match a selector
hash. See Mongo docs for details.
Options:
- :fields
-
Array of collection field names; only those will be returned (plus _id if defined)
- :offset
-
Start at this record when returning records
- :limit
-
Maximum number of records to return
- :sort
-
Either hash of field names as keys and 1/-1 as values; 1 == ascending, -1 == descending, or array of field names (all assumed to be sorted in ascending order).
- :hint
-
See #hint. This option overrides the collection-wide value.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongo/collection.rb', line 52 def find(selector={}, ={}) fields = .delete(:fields) fields = nil if fields && fields.empty? offset = .delete(:offset) || 0 limit = .delete(:limit) || 0 sort = .delete(:sort) hint = .delete(:hint) if hint hint = normalize_hint_fields(hint) else hint = @hint # assumed to be normalized already end raise RuntimeError, "Unknown options [#{.inspect}]" unless .empty? @db.query(self, Query.new(selector, fields, offset, limit, sort, hint)) end |
#find_first(selector = {}, options = {}) ⇒ Object
Find the first record that matches selector
. See #find.
69 70 71 72 73 74 |
# File 'lib/mongo/collection.rb', line 69 def find_first(selector={}, ={}) h = .dup h[:limit] = 1 cursor = find(selector, h) cursor.next_object # don't need to explicitly close b/c of limit end |
#index_information ⇒ Object
Return an array of hashes, one for each index. Each hash contains:
- :name
-
Index name
- :keys
-
Hash whose keys are the names of the fields that make up the key and values are integers.
- :ns
-
Namespace; same as this collection’s name.
147 148 149 |
# File 'lib/mongo/collection.rb', line 147 def index_information @db.index_information(@name) end |
#insert(*objects) ⇒ Object Also known as: <<
Insert objects
, which are hashes. “<<” is aliased to this method. Returns either the single inserted object or a new array containing objects
. The object(s) may have been modified by the database’s PK factory, if it has one.
80 81 82 83 84 |
# File 'lib/mongo/collection.rb', line 80 def insert(*objects) objects = objects.first if objects.size == 1 && objects.first.is_a?(Array) res = @db.insert_into_db(@name, objects) res.size > 1 ? res : res.first end |
#modify(selector, modifier_obj) ⇒ Object
Update records that match selector
by applying obj
as an update. Both selector
and modifier_obj
are required.
110 111 112 113 114 |
# File 'lib/mongo/collection.rb', line 110 def modify(selector, modifier_obj) raise "no object" unless modifier_obj raise "no selector" unless selector @db.modify_in_db(@name, selector, modifier_obj) end |
#options ⇒ Object
Return a hash containing options that apply to this collection. ‘create’ will be the collection name. For the other possible keys and values, see DB#create_collection.
154 155 156 |
# File 'lib/mongo/collection.rb', line 154 def @db.collections_info(@name).next_object()['options'] end |
#remove(selector = {}) ⇒ Object
Remove the records that match selector
.
88 89 90 |
# File 'lib/mongo/collection.rb', line 88 def remove(selector={}) @db.remove_from_db(@name, selector) end |
#replace(selector, obj) ⇒ Object
Update records that match selector
by applying obj
as an update.
104 105 106 |
# File 'lib/mongo/collection.rb', line 104 def replace(selector, obj) @db.replace_in_db(@name, selector, obj) end |
#repsert(selector, obj) ⇒ Object
Update records that match selector
by applying obj
as an update. If no match, inserts (???).
99 100 101 |
# File 'lib/mongo/collection.rb', line 99 def repsert(selector, obj) @db.repsert_in_db(@name, selector, obj) end |