Class: Monga::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/monga/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, name) ⇒ Database

Returns a new instance of Database.



5
6
7
8
# File 'lib/monga/database.rb', line 5

def initialize(client, name)
  @client = client
  @name = name
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/monga/database.rb', line 3

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/monga/database.rb', line 3

def name
  @name
end

Instance Method Details

#aggregate(collection_name, pipeline, &blk) ⇒ Object

Run aggregate command.



185
186
187
188
189
190
# File 'lib/monga/database.rb', line 185

def aggregate(collection_name, pipeline, &blk)
  cmd = {}
  cmd[:aggregate] = collection_name
  cmd[:pipeline] = pipeline
  run_cmd(cmd, blk)
end

#cmd(cmd, resp_blk = nil, &ret_blk) ⇒ Object

Run some command

cmd = { getLastError: 1 }
db.cmd(cmd){ |err, resp| ... }


28
29
30
31
32
33
34
# File 'lib/monga/database.rb', line 28

def cmd(cmd, resp_blk = nil, &ret_blk)
  if resp_blk
    run_cmd(cmd, ret_blk, &resp_blk)
  else
    run_cmd(cmd, ret_blk)
  end
end

#count(collection_name, opts = {}, &blk) ⇒ Object

Counts amount of documents in collection

db.count("myCollection"){ |err, cnt| ... }
# same as
collection = db["myCollection"]
collection.count{ |err, cnt| ... }


130
131
132
133
134
135
136
137
# File 'lib/monga/database.rb', line 130

def count(collection_name, opts = {}, &blk)
  cmd = {}
  cmd[:count] = collection_name
  cmd.merge!(opts)
  run_cmd(cmd, blk) do |resp|
    resp["n"].to_i
  end
end

#create_collection(collection_name, opts = {}, &blk) ⇒ Object

Create collection.

db.create_collection("myCollection"){ |err, resp| ... }
db.create_collection("myCappedCollection", capped: true, size: 1024*10){ |err, resp| ... }


116
117
118
119
120
121
# File 'lib/monga/database.rb', line 116

def create_collection(collection_name, opts = {}, &blk)
  cmd = {}
  cmd[:create] = collection_name
  cmd.merge!(opts)
  run_cmd(cmd, blk)
end

#distinct(collection_name, opts, &blk) ⇒ Object

Run distinct command. You should pass collection_name and key. Query option is optional.



196
197
198
199
200
201
# File 'lib/monga/database.rb', line 196

def distinct(collection_name, opts, &blk)
  cmd = {}
  cmd[:distinct] = collection_name
  cmd.merge! opts
  run_cmd(cmd, blk)
end

#drop(&blk) ⇒ Object

Drop current database

db.drop


105
106
107
108
109
# File 'lib/monga/database.rb', line 105

def drop(&blk)
  cmd = {}
  cmd[:dropDatabase] = 1
  run_cmd(cmd, blk)
end

#drop_collection(collection_name, &blk) ⇒ Object

Obviously dropping collection There is collection#drop helper exists

db.drop_collection("testCollection"){ |err, resp| ... }
# same as
collection = db["testCollection"]
collection.drop{ |err, resp| ... }


95
96
97
98
99
# File 'lib/monga/database.rb', line 95

def drop_collection(collection_name, &blk)
  cmd = {}
  cmd[:drop] = collection_name
  run_cmd(cmd, blk)
end

#drop_indexes(collection_name, indexes, &blk) ⇒ Object

Drop choosen indexes. There is collection#drop_index and collection#drop_indexes methods available

db.drop_indexes("myCollection", { title: 1 })
db.drop_indexes("myCollection", [{ title: 1 }, { author: 1 }])
# drop all indexes
db.drop_indexes("myCollection", "*")
# same as
collection = db["myCollection"]
collection.drop_index(title: 1)
# drop all indexes
collection.drop_indexes


152
153
154
155
156
157
# File 'lib/monga/database.rb', line 152

def drop_indexes(collection_name, indexes, &blk)
  cmd = {}
  cmd[:dropIndexes] = collection_name
  cmd[:index] = indexes
  run_cmd(cmd, blk)
end

#eval(js, &blk) ⇒ Object

Evaluate some raw javascript

db.eval("return('Hello World!')") do |err, resp|
  # processing
end


42
43
44
45
46
# File 'lib/monga/database.rb', line 42

def eval(js, &blk)
  cmd = {}
  cmd[:eval] = js
  run_cmd(cmd, blk)
end

#get_collection(collection_name) ⇒ Object Also known as: []

Choose collection to work

client = Monga:::Client.new
db = client.get_db("dbTest")
collection = db.get_collection("testCollection")
# same as
collection = db["testCollection"]


18
19
20
# File 'lib/monga/database.rb', line 18

def get_collection(collection_name)
  Monga::Collection.new(self, collection_name)
end

#get_last_error(connection, opts = {}, &blk) ⇒ Object

You should be cearfull with this method ‘cause it is importaint to know in wich connection you are trying to get last error information. So be happy to use safe_* methods and it will choose right connection for you. Or, if you really want to do it mannually:

request = collection.insert({ title: "Test" })
conn = request.connection
db.get_last_error(conn){ |err, resp| ... }
db.get_last_error(conn){ |err, resp| ... }
# you should pass following options:
db.get_last_error(
  conn, 
  j: true, 
  w: 2, 
  fsync: true, 
  wtimout: 100){ |err, resp| ... }


65
66
67
68
69
# File 'lib/monga/database.rb', line 65

def get_last_error(connection, opts = {}, &blk)
  raise_last_error(connection, opts, &blk)
rescue => e
  return e
end

#group(collection_name, opts, &blk) ⇒ Object

Run group command. Available options are:

key – Specifies one or more document fields to group
$reduce – Specifies an aggregation function that operates on the documents during the grouping operation
initial – Initializes the aggregation result document
$keyf – Specifies a function that creates a “key object” for use as the grouping key
cond – Specifies the selection criteria to determine which documents in the collection to process
finalize – Specifies a function that runs each item in the result


212
213
214
215
216
217
# File 'lib/monga/database.rb', line 212

def group(collection_name, opts, &blk)
  cmd = {}
  cmd[:group] = opts
  cmd[:group][:ns] ||= collection_name
  run_cmd(cmd, blk)
end

#list_collections(&blk) ⇒ Object

Just helper to show all list of collections

db.list_collections{ |err, list| ... }


238
239
240
# File 'lib/monga/database.rb', line 238

def list_collections(&blk)
  eval("db.getCollectionNames()", &blk)
end

#map_reduce(collection_name, opts, &blk) ⇒ Object

Run mapReduce command. Available options:

* map - A JavaScript function that associates or “maps” a value with a key and emits the key and value pair.
* reduce - A JavaScript function that “reduces” to a single object all the values associated with a particular key.
* out - Specifies the location of the result of the map-reduce operation.
* query - Specifies the selection criteria.
* sort - Sorts the input documents.
* limit - Specifies a maximum number of documents to return from the collection
* finalize
* scope
* jsMode
* verbose

Inline response returned by default.



175
176
177
178
179
180
181
# File 'lib/monga/database.rb', line 175

def map_reduce(collection_name, opts, &blk)
  cmd = {}
  cmd[:mapReduce] = collection_name
  cmd.merge! opts
  cmd[:out] ||= { inline: 1 }
  run_cmd(cmd, blk)
end

#raise_last_error(connection, opts = {}, &blk) ⇒ Object

Instead of get_last_eror this one will actually raise it. It is usefull for safe_* methods who should raise an error if something goes wrong



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/monga/database.rb', line 74

def raise_last_error(connection, opts = {}, &blk)
  cmd = {}
  cmd[:getLastError] = 1
  cmd[:connection] = connection

  cmd[:j] = opts[:j] if opts[:j]
  cmd[:fsync] = opts[:fsync] if opts[:fsync]
  cmd[:w] = opts[:w] if opts[:w]
  cmd[:wtimeout] = opts[:wtimeout] if opts[:wtimeout]

  run_cmd(cmd, blk)
end

#text(collection_name, opts, &blk) ⇒ Object

Run text command. Available options are:

search (string) – A string of terms that MongoDB parses and uses to query the text index
filter (document) – A query document to further limit the results of the query using another database field
project (document) – Allows you to limit the fields returned by the query to only those specified.
limit (number) – Specify the maximum number of documents to include in the response
language (string) – Specify the language that determines for the search the list of stop words and the rules for the stemmer and tokenizer


227
228
229
230
231
232
# File 'lib/monga/database.rb', line 227

def text(collection_name, opts, &blk)
  cmd = {}
  cmd[:text] = collection_name
  cmd.merge! opts
  run_cmd(cmd, blk)
end