2010-05-07 19:33:27 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
2008-12-17 16:49:06 +00:00
|
|
|
# --
|
2010-02-19 22:41:36 +00:00
|
|
|
# Copyright (C) 2008-2010 10gen Inc.
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-22 01:00:51 +00:00
|
|
|
#
|
2009-02-15 13:24:14 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2008-12-17 16:49:06 +00:00
|
|
|
# ++
|
2008-11-22 01:00:51 +00:00
|
|
|
|
|
|
|
require 'socket'
|
2009-11-20 22:48:41 +00:00
|
|
|
require 'timeout'
|
2009-10-19 15:46:59 +00:00
|
|
|
require 'thread'
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
module Mongo
|
2009-01-16 21:10:52 +00:00
|
|
|
|
2010-01-09 00:11:49 +00:00
|
|
|
# A MongoDB database.
|
2009-08-20 14:50:48 +00:00
|
|
|
class DB
|
2009-08-13 20:26:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"
|
|
|
|
SYSTEM_INDEX_COLLECTION = "system.indexes"
|
|
|
|
SYSTEM_PROFILE_COLLECTION = "system.profile"
|
|
|
|
SYSTEM_USER_COLLECTION = "system.users"
|
2010-07-03 18:24:24 +00:00
|
|
|
SYSTEM_JS_COLLECTION = "system.js"
|
2009-08-20 14:50:48 +00:00
|
|
|
SYSTEM_COMMAND_COLLECTION = "$cmd"
|
2009-08-13 20:26:51 +00:00
|
|
|
|
2009-10-19 21:14:41 +00:00
|
|
|
# Counter for generating unique request ids.
|
|
|
|
@@current_request_id = 0
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Strict mode enforces collection existence checks. When +true+,
|
2009-11-23 20:20:05 +00:00
|
|
|
# asking for a collection that does not exist, or trying to create a
|
|
|
|
# collection that already exists, raises an error.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2009-11-23 20:20:05 +00:00
|
|
|
# Strict mode is disabled by default, but enabled (+true+) at any time.
|
2009-08-20 14:50:48 +00:00
|
|
|
attr_writer :strict
|
2009-01-14 23:37:28 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Returns the value of the +strict+ flag.
|
|
|
|
def strict?; @strict; end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# The name of the database.
|
|
|
|
attr_reader :name
|
2009-01-21 16:26:18 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
# The Mongo::Connection instance connecting to the MongoDB server.
|
2009-11-04 22:46:15 +00:00
|
|
|
attr_reader :connection
|
2009-12-16 19:03:15 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Instances of DB are normally obtained by calling Mongo#db.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @param [String] db_name the database name.
|
|
|
|
# @param [Mongo::Connection] connection a connection object pointing to MongoDB. Note
|
|
|
|
# that databases are usually instantiated via the Connection class. See the examples below.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-03-30 17:35:36 +00:00
|
|
|
# @option options [Boolean] :strict (False) If true, collections must exist to be accessed and must
|
2010-01-11 23:11:38 +00:00
|
|
|
# not exist to be created. See DB#collection and DB#create_collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-03-30 17:33:03 +00:00
|
|
|
# @option options [Object, #create_pk(doc)] :pk (Mongo::ObjectID) A primary key factory object,
|
2010-01-11 23:11:38 +00:00
|
|
|
# which should take a hash and return a hash which merges the original hash with any primary key
|
|
|
|
# fields the factory wishes to inject. (NOTE: if the object already has a primary key,
|
|
|
|
# the factory should not inject a new key).
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core databases constructor_details
|
2009-11-23 20:20:05 +00:00
|
|
|
def initialize(db_name, connection, options={})
|
2010-03-16 17:56:30 +00:00
|
|
|
@name = Mongo::Support.validate_db_name(db_name)
|
2009-11-23 20:20:05 +00:00
|
|
|
@connection = connection
|
|
|
|
@strict = options[:strict]
|
2009-08-20 14:50:48 +00:00
|
|
|
@pk_factory = options[:pk]
|
|
|
|
end
|
2008-12-16 22:35:31 +00:00
|
|
|
|
2010-01-13 18:11:09 +00:00
|
|
|
# Authenticate with the given username and password. Note that mongod
|
2010-01-11 23:11:38 +00:00
|
|
|
# must be started with the --auth option for authentication to be enabled.
|
|
|
|
#
|
|
|
|
# @param [String] username
|
|
|
|
# @param [String] password
|
2010-03-01 15:39:50 +00:00
|
|
|
# @param [Boolean] save_auth
|
|
|
|
# Save this authentication to the connection object using Connection#add_auth. This
|
|
|
|
# will ensure that the authentication will be applied on database reconnect.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
2010-02-24 18:05:45 +00:00
|
|
|
# @raise [AuthenticationError]
|
|
|
|
#
|
2010-02-08 17:12:18 +00:00
|
|
|
# @core authenticate authenticate-instance_method
|
2010-03-01 15:39:50 +00:00
|
|
|
def authenticate(username, password, save_auth=true)
|
2010-05-24 15:43:02 +00:00
|
|
|
doc = command({:getnonce => 1}, :check_response => false)
|
2009-08-20 14:50:48 +00:00
|
|
|
raise "error retrieving nonce: #{doc}" unless ok?(doc)
|
|
|
|
nonce = doc['nonce']
|
|
|
|
|
2010-05-07 01:25:18 +00:00
|
|
|
auth = BSON::OrderedHash.new
|
2009-08-20 14:50:48 +00:00
|
|
|
auth['authenticate'] = 1
|
|
|
|
auth['user'] = username
|
|
|
|
auth['nonce'] = nonce
|
2010-03-16 17:56:30 +00:00
|
|
|
auth['key'] = Mongo::Support.auth_key(username, password, nonce)
|
2010-05-18 20:17:17 +00:00
|
|
|
if ok?(self.command(auth, :check_response => false))
|
2010-03-01 17:18:57 +00:00
|
|
|
if save_auth
|
2010-02-25 19:58:32 +00:00
|
|
|
@connection.add_auth(@name, username, password)
|
|
|
|
end
|
|
|
|
true
|
|
|
|
else
|
|
|
|
raise(Mongo::AuthenticationError, "Failed to authenticate user '#{username}' on db '#{self.name}'")
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2010-07-03 18:24:24 +00:00
|
|
|
# Adds a stored Javascript function to the database which can executed
|
|
|
|
# server-side in map_reduce, db.eval and $where clauses.
|
|
|
|
#
|
|
|
|
# @param [String] function_name
|
|
|
|
# @param [String] code
|
|
|
|
#
|
|
|
|
# @return [String] the function name saved to the database
|
|
|
|
def add_stored_function(function_name, code)
|
|
|
|
self[SYSTEM_JS_COLLECTION].save(
|
|
|
|
{
|
|
|
|
"_id" => function_name,
|
|
|
|
:value => BSON::Code.new(code)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2010-08-04 20:26:29 +00:00
|
|
|
|
2010-07-03 18:24:24 +00:00
|
|
|
# Removes stored Javascript function from the database. Returns
|
|
|
|
# false if the function does not exist
|
|
|
|
#
|
|
|
|
# @param [String] function_name
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def remove_stored_function(function_name)
|
|
|
|
if self[SYSTEM_JS_COLLECTION].find_one({"_id" => function_name})
|
|
|
|
self[SYSTEM_JS_COLLECTION].remove({"_id" => function_name}, :safe => true)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2010-08-04 20:26:29 +00:00
|
|
|
|
2010-01-28 21:39:40 +00:00
|
|
|
# Adds a user to this database for use with authentication. If the user already
|
|
|
|
# exists in the system, the password will be updated.
|
|
|
|
#
|
|
|
|
# @param [String] username
|
|
|
|
# @param [String] password
|
|
|
|
#
|
|
|
|
# @return [Hash] an object representing the user.
|
|
|
|
def add_user(username, password)
|
|
|
|
users = self[SYSTEM_USER_COLLECTION]
|
|
|
|
user = users.find_one({:user => username}) || {:user => username}
|
2010-03-16 17:56:30 +00:00
|
|
|
user['pwd'] = Mongo::Support.hash_password(username, password)
|
2010-01-28 21:39:40 +00:00
|
|
|
users.save(user)
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove the given user from this database. Returns false if the user
|
|
|
|
# doesn't exist in the system.
|
|
|
|
#
|
|
|
|
# @param [String] username
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def remove_user(username)
|
|
|
|
if self[SYSTEM_USER_COLLECTION].find_one({:user => username})
|
|
|
|
self[SYSTEM_USER_COLLECTION].remove({:user => username}, :safe => true)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-25 19:58:32 +00:00
|
|
|
# Deauthorizes use for this database for this connection. Also removes
|
|
|
|
# any saved authorization in the connection class associated with this
|
|
|
|
# database.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @raise [MongoDBError] if logging out fails.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2009-08-20 14:50:48 +00:00
|
|
|
def logout
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(:logout => 1)
|
2010-02-25 19:58:32 +00:00
|
|
|
if ok?(doc)
|
|
|
|
@connection.remove_auth(@name)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
raise MongoDBError, "error logging out: #{doc.inspect}"
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-23 18:47:27 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get an array of collection names in this database.
|
|
|
|
#
|
|
|
|
# @return [Array]
|
2009-08-20 14:50:48 +00:00
|
|
|
def collection_names
|
|
|
|
names = collections_info.collect { |doc| doc['name'] || '' }
|
|
|
|
names = names.delete_if {|name| name.index(@name).nil? || name.index('$')}
|
|
|
|
names.map {|name| name.sub(@name + '.', '')}
|
|
|
|
end
|
2009-05-26 19:26:20 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get an array of Collection instances, one for each collection in this database.
|
|
|
|
#
|
|
|
|
# @return [Array<Mongo::Collection>]
|
2009-08-27 20:29:41 +00:00
|
|
|
def collections
|
|
|
|
collection_names.map do |collection_name|
|
|
|
|
Collection.new(self, collection_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get info on system namespaces (collections). This method returns
|
|
|
|
# a cursor which can be iterated over. For each collection, a hash
|
|
|
|
# will be yielded containing a 'name' string and, optionally, an 'options' hash.
|
|
|
|
#
|
|
|
|
# @param [String] coll_name return info for the specifed collection only.
|
|
|
|
#
|
2010-01-13 18:11:09 +00:00
|
|
|
# @return [Mongo::Cursor]
|
2009-08-20 14:50:48 +00:00
|
|
|
def collections_info(coll_name=nil)
|
|
|
|
selector = {}
|
2009-10-19 21:14:41 +00:00
|
|
|
selector[:name] = full_collection_name(coll_name) if coll_name
|
2009-10-22 18:10:12 +00:00
|
|
|
Cursor.new(Collection.new(self, SYSTEM_NAMESPACE_COLLECTION), :selector => selector)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-05-26 19:26:20 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Create a collection.
|
|
|
|
#
|
2009-08-20 14:50:48 +00:00
|
|
|
# new collection. If +strict+ is true, will raise an error if
|
|
|
|
# collection +name+ already exists.
|
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @param [String] name the name of the new collection.
|
|
|
|
#
|
|
|
|
# @option options [Boolean] :capped (False) created a capped collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @option options [Integer] :size (Nil) If +capped+ is +true+, specifies the maximum number of
|
|
|
|
# bytes for the capped collection. If +false+, specifies the number of bytes allocated
|
|
|
|
# for the initial extent of the collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @option options [Integer] :max (Nil) If +capped+ is +true+, indicates the maximum number of records
|
|
|
|
# in a capped collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @raise [MongoDBError] raised under two conditions: either we're in +strict+ mode and the collection
|
|
|
|
# already exists or collection creation fails on the server.
|
|
|
|
#
|
|
|
|
# @return [Mongo::Collection]
|
2009-08-20 14:50:48 +00:00
|
|
|
def create_collection(name, options={})
|
2010-01-11 23:11:38 +00:00
|
|
|
# Does the collection already exist?
|
2009-08-20 14:50:48 +00:00
|
|
|
if collection_names.include?(name)
|
|
|
|
if strict?
|
2010-01-11 23:11:38 +00:00
|
|
|
raise MongoDBError, "Collection #{name} already exists. Currently in strict mode."
|
2009-08-20 14:50:48 +00:00
|
|
|
else
|
|
|
|
return Collection.new(self, name)
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Create a new collection.
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2009-08-20 14:50:48 +00:00
|
|
|
oh[:create] = name
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh.merge(options || {}))
|
2010-06-01 02:52:03 +00:00
|
|
|
return Collection.new(self, name, @pk_factory) if ok?(doc)
|
2010-01-11 23:11:38 +00:00
|
|
|
raise MongoDBError, "Error creating collection: #{doc.inspect}"
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-13 18:08:04 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get a collection by name.
|
|
|
|
#
|
|
|
|
# @param [String] name the collection name.
|
|
|
|
#
|
|
|
|
# @raise [MongoDBError] if collection does not already exist and we're in +strict+ mode.
|
2010-01-13 18:11:09 +00:00
|
|
|
#
|
|
|
|
# @return [Mongo::Collection]
|
2009-08-20 14:50:48 +00:00
|
|
|
def collection(name)
|
2009-10-19 21:14:41 +00:00
|
|
|
return Collection.new(self, name, @pk_factory) if !strict? || collection_names.include?(name)
|
2010-04-05 15:07:01 +00:00
|
|
|
raise Mongo::MongoDBError, "Collection #{name} doesn't exist. Currently in strict mode."
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
|
|
|
alias_method :[], :collection
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Drop a collection by +name+.
|
|
|
|
#
|
|
|
|
# @param [String] name
|
|
|
|
#
|
2010-07-12 17:51:40 +00:00
|
|
|
# @return [Boolean] +true+ on success or +false+ if the collection name doesn't exist.
|
2009-08-20 14:50:48 +00:00
|
|
|
def drop_collection(name)
|
|
|
|
return true unless collection_names.include?(name)
|
2009-03-04 19:28:00 +00:00
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
ok?(command(:drop => name))
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-02-26 17:06:03 +00:00
|
|
|
|
2010-08-02 22:19:54 +00:00
|
|
|
# @deprecated
|
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get the error message from the most recently executed database
|
2010-01-13 18:11:09 +00:00
|
|
|
# operation for this connection.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
2010-05-04 20:00:05 +00:00
|
|
|
# @option opts [Boolean] :fsync (false)
|
|
|
|
# @option opts [Integer] :w (nil)
|
|
|
|
# @option opts [Integer] :wtimeout (nil)
|
|
|
|
#
|
|
|
|
# @return [String, Nil] either the text describing an error or nil if no
|
2010-01-13 18:11:09 +00:00
|
|
|
# error has occurred.
|
2010-05-04 20:00:05 +00:00
|
|
|
def error(opts={})
|
2010-08-02 22:19:54 +00:00
|
|
|
warn "DB#error is deprecated. Please use DB#get_last_error instead"
|
2010-05-04 20:00:05 +00:00
|
|
|
opts.assert_valid_keys(:w, :wtimeout, :fsync)
|
2010-08-02 22:19:54 +00:00
|
|
|
get_last_error(opts)['err']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run the getlasterror command with the specified replication options.
|
|
|
|
#
|
|
|
|
# @option opts [Boolean] :fsync (false)
|
|
|
|
# @option opts [Integer] :w (nil)
|
|
|
|
# @option opts [Integer] :wtimeout (nil)
|
|
|
|
#
|
|
|
|
# @return [Hash] the entire response to getlasterror.
|
|
|
|
#
|
|
|
|
# @raise [MongoDBError] if the operation fails.
|
|
|
|
def get_last_error(opts={})
|
2010-05-07 01:25:18 +00:00
|
|
|
cmd = BSON::OrderedHash.new
|
2010-05-04 20:00:05 +00:00
|
|
|
cmd[:getlasterror] = 1
|
2010-08-02 22:19:54 +00:00
|
|
|
cmd.merge!(opts)
|
2010-05-18 20:17:17 +00:00
|
|
|
doc = command(cmd, :check_response => false)
|
2010-05-04 20:00:05 +00:00
|
|
|
raise MongoDBError, "error retrieving last error: #{doc.inspect}" unless ok?(doc)
|
2010-08-02 22:19:54 +00:00
|
|
|
doc
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-13 19:02:16 +00:00
|
|
|
|
2010-08-02 22:19:54 +00:00
|
|
|
# @deprecated
|
|
|
|
#
|
2009-10-01 16:01:37 +00:00
|
|
|
# Get status information from the last operation on this connection.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Hash] a hash representing the status of the last db op.
|
2009-10-01 16:01:37 +00:00
|
|
|
def last_status
|
2010-08-02 22:19:54 +00:00
|
|
|
warn "DB#last_status is deprecated. Please use the equivalent DB#get_last_error instead"
|
2009-11-23 20:20:05 +00:00
|
|
|
command(:getlasterror => 1)
|
2009-10-01 16:01:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Return +true+ if an error was caused by the most recently executed
|
2009-08-20 14:50:48 +00:00
|
|
|
# database operation.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2009-08-20 14:50:48 +00:00
|
|
|
def error?
|
2010-08-04 20:21:41 +00:00
|
|
|
get_last_error['err'] != nil
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get the most recent error to have occured on this database.
|
2009-08-20 14:50:48 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# This command only returns errors that have occured since the last call to
|
2009-08-20 14:50:48 +00:00
|
|
|
# DB#reset_error_history - returns +nil+ if there is no such error.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
2010-01-13 18:11:09 +00:00
|
|
|
# @return [String, Nil] the text of the error or +nil+ if no error has occurred.
|
2009-08-20 14:50:48 +00:00
|
|
|
def previous_error
|
2009-11-23 20:20:05 +00:00
|
|
|
error = command(:getpreverror => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
if error["err"]
|
|
|
|
error
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
# Reset the error history of this database
|
|
|
|
#
|
|
|
|
# Calls to DB#previous_error will only return errors that have occurred
|
|
|
|
# since the most recent call to this method.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Hash]
|
2009-08-20 14:50:48 +00:00
|
|
|
def reset_error_history
|
2009-11-23 20:20:05 +00:00
|
|
|
command(:reseterror => 1)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-04-28 18:55:36 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Dereference a DBRef, returning the document it points to.
|
|
|
|
#
|
|
|
|
# @param [Mongo::DBRef] dbref
|
|
|
|
#
|
|
|
|
# @return [Hash] the document indicated by the db reference.
|
|
|
|
#
|
|
|
|
# @see http://www.mongodb.org/display/DOCS/DB+Ref MongoDB DBRef spec.
|
2009-08-20 14:50:48 +00:00
|
|
|
def dereference(dbref)
|
|
|
|
collection(dbref.namespace).find_one("_id" => dbref.object_id)
|
|
|
|
end
|
2008-12-16 22:35:31 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Evaluate a JavaScript expression in MongoDB.
|
|
|
|
#
|
|
|
|
# @param [String, Code] code a JavaScript expression to evaluate server-side.
|
|
|
|
# @param [Integer, Hash] args any additional argument to be passed to the +code+ expression when
|
|
|
|
# it's run on the server.
|
2010-01-13 18:11:09 +00:00
|
|
|
#
|
|
|
|
# @return [String] the return value of the function.
|
2009-08-20 14:50:48 +00:00
|
|
|
def eval(code, *args)
|
2010-04-05 15:07:01 +00:00
|
|
|
if not code.is_a? BSON::Code
|
|
|
|
code = BSON::Code.new(code)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-12-08 16:38:42 +00:00
|
|
|
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2009-08-20 14:50:48 +00:00
|
|
|
oh[:$eval] = code
|
2010-08-04 20:21:41 +00:00
|
|
|
oh[:args] = args
|
2009-11-23 20:20:05 +00:00
|
|
|
doc = command(oh)
|
2010-08-04 20:21:41 +00:00
|
|
|
doc['retval']
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Rename a collection.
|
|
|
|
#
|
|
|
|
# @param [String] from original collection name.
|
|
|
|
# @param [String] to new collection name.
|
|
|
|
#
|
|
|
|
# @return [True] returns +true+ on success.
|
2010-08-04 20:26:29 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @raise MongoDBError if there's an error renaming the collection.
|
2009-08-20 14:50:48 +00:00
|
|
|
def rename_collection(from, to)
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2009-08-20 14:50:48 +00:00
|
|
|
oh[:renameCollection] = "#{@name}.#{from}"
|
|
|
|
oh[:to] = "#{@name}.#{to}"
|
2010-08-04 20:21:41 +00:00
|
|
|
doc = DB.new('admin', @connection).command(oh, :check_response => false)
|
2010-01-11 23:11:38 +00:00
|
|
|
ok?(doc) || raise(MongoDBError, "Error renaming collection: #{doc.inspect}")
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-08-10 20:10:52 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Drop an index from a given collection. Normally called from
|
2009-08-20 14:50:48 +00:00
|
|
|
# Collection#drop_index or Collection#drop_indexes.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @param [String] collection_name
|
|
|
|
# @param [String] index_name
|
|
|
|
#
|
|
|
|
# @return [True] returns +true+ on success.
|
|
|
|
#
|
|
|
|
# @raise MongoDBError if there's an error renaming the collection.
|
|
|
|
def drop_index(collection_name, index_name)
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2009-08-20 14:50:48 +00:00
|
|
|
oh[:deleteIndexes] = collection_name
|
2010-01-11 23:11:38 +00:00
|
|
|
oh[:index] = index_name
|
2010-07-12 18:31:12 +00:00
|
|
|
doc = command(oh, :check_response => false)
|
2010-01-11 23:11:38 +00:00
|
|
|
ok?(doc) || raise(MongoDBError, "Error with drop_index command: #{doc.inspect}")
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2009-01-21 16:26:18 +00:00
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get information on the indexes for the given collection.
|
|
|
|
# Normally called by Collection#index_information.
|
|
|
|
#
|
|
|
|
# @param [String] collection_name
|
|
|
|
#
|
|
|
|
# @return [Hash] keys are index names and the values are lists of [key, direction] pairs
|
|
|
|
# defining the index.
|
2009-08-20 14:50:48 +00:00
|
|
|
def index_information(collection_name)
|
2010-03-16 19:59:26 +00:00
|
|
|
sel = {:ns => full_collection_name(collection_name)}
|
2009-08-20 14:50:48 +00:00
|
|
|
info = {}
|
2010-03-16 19:59:26 +00:00
|
|
|
Cursor.new(Collection.new(self, SYSTEM_INDEX_COLLECTION), :selector => sel).each do |index|
|
|
|
|
info[index['name']] = index
|
|
|
|
end
|
2009-08-20 14:50:48 +00:00
|
|
|
info
|
|
|
|
end
|
|
|
|
|
2010-04-06 22:29:39 +00:00
|
|
|
# Return stats on this database. Uses MongoDB's dbstats command.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def stats
|
|
|
|
self.command({:dbstats => 1})
|
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Return +true+ if the supplied +doc+ contains an 'ok' field with the value 1.
|
|
|
|
#
|
|
|
|
# @param [Hash] doc
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2009-11-05 21:14:37 +00:00
|
|
|
def ok?(doc)
|
2010-06-01 02:52:03 +00:00
|
|
|
Mongo::Support.ok?(doc)
|
2009-11-05 21:14:37 +00:00
|
|
|
end
|
|
|
|
|
2010-01-13 18:11:09 +00:00
|
|
|
# Send a command to the database.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# Note: DB commands must start with the "command" key. For this reason,
|
|
|
|
# any selector containing more than one key must be an OrderedHash.
|
2009-11-05 21:14:37 +00:00
|
|
|
#
|
2010-03-02 17:24:52 +00:00
|
|
|
# Note also that a command in MongoDB is just a kind of query
|
|
|
|
# that occurs on the system command collection ($cmd). Examine this method's implementation
|
|
|
|
# to see how it works.
|
2010-01-13 18:11:09 +00:00
|
|
|
#
|
2010-01-11 23:11:38 +00:00
|
|
|
# @param [OrderedHash, Hash] selector an OrderedHash, or a standard Hash with just one
|
2010-05-18 20:17:17 +00:00
|
|
|
# key, specifying the command to be performed. In Ruby 1.9, OrderedHash isn't necessary since
|
|
|
|
# hashes are ordered by default.
|
2009-11-05 21:14:37 +00:00
|
|
|
#
|
2010-05-18 20:17:17 +00:00
|
|
|
# @option opts [Boolean] :check_response (true) If +true+, raises an exception if the
|
2009-11-05 21:14:37 +00:00
|
|
|
# command fails.
|
2010-05-18 20:17:17 +00:00
|
|
|
# @option opts [Socket] :sock a socket to use for sending the command. This is mainly for internal use.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Hash]
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core commands command_instance-method
|
2010-08-04 20:26:29 +00:00
|
|
|
def command(selector, opts={})
|
|
|
|
check_response = opts.fetch(:check_response, true)
|
2010-07-12 18:31:12 +00:00
|
|
|
sock = opts[:sock]
|
2009-11-05 21:14:37 +00:00
|
|
|
raise MongoArgumentError, "command must be given a selector" unless selector.is_a?(Hash) && !selector.empty?
|
2010-05-24 15:43:02 +00:00
|
|
|
if selector.keys.length > 1 && RUBY_VERSION < '1.9' && selector.class != BSON::OrderedHash
|
2009-11-05 21:14:37 +00:00
|
|
|
raise MongoArgumentError, "DB#command requires an OrderedHash when hash contains multiple keys"
|
|
|
|
end
|
|
|
|
|
2010-07-12 18:31:12 +00:00
|
|
|
result = Cursor.new(system_command_collection,
|
2009-12-16 19:03:15 +00:00
|
|
|
:limit => -1, :selector => selector, :socket => sock).next_document
|
2009-11-05 21:14:37 +00:00
|
|
|
|
2010-07-12 18:33:52 +00:00
|
|
|
if result.nil?
|
|
|
|
raise OperationFailure, "Database command '#{selector.keys.first}' failed: returned null."
|
|
|
|
elsif (check_response && !ok?(result))
|
2010-03-16 17:56:30 +00:00
|
|
|
raise OperationFailure, "Database command '#{selector.keys.first}' failed: #{result.inspect}"
|
2009-11-05 21:14:37 +00:00
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# A shortcut returning db plus dot plus collection name.
|
|
|
|
#
|
|
|
|
# @param [String] collection_name
|
|
|
|
#
|
|
|
|
# @return [String]
|
2009-11-05 21:14:37 +00:00
|
|
|
def full_collection_name(collection_name)
|
|
|
|
"#{@name}.#{collection_name}"
|
|
|
|
end
|
|
|
|
|
2010-01-09 00:11:49 +00:00
|
|
|
# The primary key factory object (or +nil+).
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @return [Object, Nil]
|
2010-01-09 00:11:49 +00:00
|
|
|
def pk_factory
|
|
|
|
@pk_factory
|
|
|
|
end
|
|
|
|
|
|
|
|
# Specify a primary key factory if not already set.
|
|
|
|
#
|
|
|
|
# @raise [MongoArgumentError] if the primary key factory has already been set.
|
|
|
|
def pk_factory=(pk_factory)
|
|
|
|
if @pk_factory
|
|
|
|
raise MongoArgumentError, "Cannot change primary key factory once it's been set"
|
|
|
|
end
|
|
|
|
|
|
|
|
@pk_factory = pk_factory
|
|
|
|
end
|
|
|
|
|
2010-01-13 18:11:09 +00:00
|
|
|
# Return the current database profiling level. If profiling is enabled, you can
|
|
|
|
# get the results using DB#profiling_info.
|
2010-01-06 18:51:12 +00:00
|
|
|
#
|
|
|
|
# @return [Symbol] :off, :slow_only, or :all
|
2010-02-08 17:12:18 +00:00
|
|
|
#
|
|
|
|
# @core profiling profiling_level-instance_method
|
2010-01-06 18:51:12 +00:00
|
|
|
def profiling_level
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2010-01-06 18:51:12 +00:00
|
|
|
oh[:profile] = -1
|
2010-08-04 20:21:41 +00:00
|
|
|
doc = command(oh, :check_response => false)
|
2010-01-06 18:51:12 +00:00
|
|
|
raise "Error with profile command: #{doc.inspect}" unless ok?(doc) && doc['was'].kind_of?(Numeric)
|
|
|
|
case doc['was'].to_i
|
|
|
|
when 0
|
|
|
|
:off
|
|
|
|
when 1
|
|
|
|
:slow_only
|
|
|
|
when 2
|
|
|
|
:all
|
|
|
|
else
|
|
|
|
raise "Error: illegal profiling level value #{doc['was']}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-13 18:11:09 +00:00
|
|
|
# Set this database's profiling level. If profiling is enabled, you can
|
|
|
|
# get the results using DB#profiling_info.
|
2010-01-11 23:11:38 +00:00
|
|
|
#
|
|
|
|
# @param [Symbol] level acceptable options are +:off+, +:slow_only+, or +:all+.
|
2010-01-06 18:51:12 +00:00
|
|
|
def profiling_level=(level)
|
2010-05-07 01:25:18 +00:00
|
|
|
oh = BSON::OrderedHash.new
|
2010-01-06 18:51:12 +00:00
|
|
|
oh[:profile] = case level
|
|
|
|
when :off
|
|
|
|
0
|
|
|
|
when :slow_only
|
|
|
|
1
|
|
|
|
when :all
|
|
|
|
2
|
|
|
|
else
|
|
|
|
raise "Error: illegal profiling level value #{level}"
|
|
|
|
end
|
2010-08-04 20:21:41 +00:00
|
|
|
doc = command(oh, :check_response => false)
|
2010-01-11 23:11:38 +00:00
|
|
|
ok?(doc) || raise(MongoDBError, "Error with profile command: #{doc.inspect}")
|
2010-01-06 18:51:12 +00:00
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Get the current profiling information.
|
|
|
|
#
|
|
|
|
# @return [Array] a list of documents containing profiling information.
|
2010-01-06 18:51:12 +00:00
|
|
|
def profiling_info
|
|
|
|
Cursor.new(Collection.new(self, DB::SYSTEM_PROFILE_COLLECTION), :selector => {}).to_a
|
|
|
|
end
|
|
|
|
|
2010-01-11 23:11:38 +00:00
|
|
|
# Validate a named collection.
|
|
|
|
#
|
|
|
|
# @param [String] name the collection name.
|
|
|
|
#
|
|
|
|
# @return [Hash] validation information.
|
|
|
|
#
|
|
|
|
# @raise [MongoDBError] if the command fails or there's a problem with the validation
|
|
|
|
# data, or if the collection is invalid.
|
2010-01-06 18:51:12 +00:00
|
|
|
def validate_collection(name)
|
2010-08-04 20:21:41 +00:00
|
|
|
doc = command({:validate => name}, :check_response => false)
|
2010-01-11 23:11:38 +00:00
|
|
|
raise MongoDBError, "Error with validate command: #{doc.inspect}" unless ok?(doc)
|
2010-01-06 18:51:12 +00:00
|
|
|
result = doc['result']
|
2010-01-11 23:11:38 +00:00
|
|
|
raise MongoDBError, "Error with validation data: #{doc.inspect}" unless result.kind_of?(String)
|
|
|
|
raise MongoDBError, "Error: invalid collection #{name}: #{doc.inspect}" if result =~ /\b(exception|corrupt)\b/i
|
2010-01-06 18:51:12 +00:00
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2009-11-05 21:14:37 +00:00
|
|
|
private
|
|
|
|
|
2009-11-23 20:20:05 +00:00
|
|
|
def system_command_collection
|
|
|
|
Collection.new(self, SYSTEM_COMMAND_COLLECTION)
|
2009-08-20 14:50:48 +00:00
|
|
|
end
|
2008-11-22 01:00:51 +00:00
|
|
|
end
|
|
|
|
end
|