DEPRECATE db._command in favor of db.command, which is now part of the documented API
This commit is contained in:
parent
0eee221ac9
commit
78c825b548
@ -227,8 +227,8 @@ class GridFile(object):
|
||||
|
||||
self.__flush_write_buffer()
|
||||
|
||||
md5 = self.__collection.database._command(SON([("filemd5", self.__id),
|
||||
("root", self.__collection.name)]))["md5"]
|
||||
md5 = self.__collection.database.command(SON([("filemd5", self.__id),
|
||||
("root", self.__collection.name)]))["md5"]
|
||||
|
||||
grid_file = self.__collection.files.find_one({"_id": self.__id})
|
||||
grid_file["md5"] = md5
|
||||
|
||||
@ -89,7 +89,7 @@ class Collection(object):
|
||||
command = SON({"create": self.__name})
|
||||
command.update(options)
|
||||
|
||||
self.__database._command(command)
|
||||
self.__database.command(command)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Get a sub-collection of this collection by name.
|
||||
@ -589,10 +589,10 @@ class Collection(object):
|
||||
|
||||
self.__database.connection._purge_index(self.__database.name,
|
||||
self.__name, name)
|
||||
self.__database._command(SON([("deleteIndexes",
|
||||
self.__name),
|
||||
("index", name)]),
|
||||
["ns not found"])
|
||||
self.__database.command(SON([("deleteIndexes",
|
||||
self.__name),
|
||||
("index", name)]),
|
||||
["ns not found"])
|
||||
|
||||
def index_information(self):
|
||||
"""Get information on this collection's indexes.
|
||||
@ -677,7 +677,7 @@ class Collection(object):
|
||||
if finalize is not None:
|
||||
group["finalize"] = Code(finalize)
|
||||
|
||||
return self.__database._command({"group":group})["retval"]
|
||||
return self.__database.command({"group":group})["retval"]
|
||||
|
||||
def rename(self, new_name):
|
||||
"""Rename this collection.
|
||||
@ -704,7 +704,7 @@ class Collection(object):
|
||||
("to", "%s.%s" % (self.__database.name,
|
||||
new_name))])
|
||||
|
||||
self.__database.connection.admin._command(rename_command)
|
||||
self.__database.connection.admin.command(rename_command)
|
||||
|
||||
def distinct(self, key):
|
||||
"""Get a list of distinct values for `key` among all documents in this
|
||||
@ -756,7 +756,7 @@ class Collection(object):
|
||||
("map", map), ("reduce", reduce)])
|
||||
command.update(**kwargs)
|
||||
|
||||
response = self.__database._command(command)
|
||||
response = self.__database.command(command)
|
||||
if full_response:
|
||||
return response
|
||||
return self.__database[response["result"]]
|
||||
|
||||
@ -216,7 +216,7 @@ class Connection(object): # TODO support auth for pooling
|
||||
|
||||
Return a tuple (host, port).
|
||||
"""
|
||||
result = self["admin"]._command({"ismaster": 1}, sock=sock)
|
||||
result = self["admin"].command({"ismaster": 1}, _sock=sock)
|
||||
|
||||
if result["ismaster"] == 1:
|
||||
return True
|
||||
@ -736,14 +736,14 @@ class Connection(object): # TODO support auth for pooling
|
||||
def __database_info(self):
|
||||
"""Get a dictionary of (database_name: size_on_disk).
|
||||
"""
|
||||
result = self["admin"]._command({"listDatabases": 1})
|
||||
result = self["admin"].command({"listDatabases": 1})
|
||||
info = result["databases"]
|
||||
return dict([(db["name"], db["sizeOnDisk"]) for db in info])
|
||||
|
||||
def server_info(self):
|
||||
"""Get information about the MongoDB server we're connected to.
|
||||
"""
|
||||
return self["admin"]._command({"buildinfo": 1})
|
||||
return self.admin.command({"buildinfo": 1})
|
||||
|
||||
def database_names(self):
|
||||
"""Get a list of the names of all databases on the connected server.
|
||||
@ -770,7 +770,7 @@ class Connection(object): # TODO support auth for pooling
|
||||
"(Database, str, unicode)")
|
||||
|
||||
self._purge_index(name)
|
||||
self[name]._command({"dropDatabase": 1})
|
||||
self[name].command({"dropDatabase": 1})
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
@ -307,8 +307,7 @@ class Cursor(object):
|
||||
if self.__skip:
|
||||
command["skip"] = self.__skip
|
||||
|
||||
response = self.__collection.database._command(command,
|
||||
["ns missing"])
|
||||
response = self.__collection.database.command(command, ["ns missing"])
|
||||
if response.get("errmsg", "") == "ns missing":
|
||||
return 0
|
||||
return int(response["n"])
|
||||
@ -337,7 +336,7 @@ class Cursor(object):
|
||||
if self.__spec:
|
||||
command["query"] = self.__spec
|
||||
|
||||
return self.__collection.database._command(command)["values"]
|
||||
return self.__collection.database.command(command)["values"]
|
||||
|
||||
def explain(self):
|
||||
"""Returns an explain plan record for this cursor.
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
"""Database level operations."""
|
||||
|
||||
import types
|
||||
import warnings
|
||||
try:
|
||||
import hashlib
|
||||
_md5func = hashlib.md5
|
||||
@ -194,9 +195,23 @@ class Database(object):
|
||||
return son
|
||||
|
||||
def _command(self, command, allowable_errors=[], check=True, sock=None):
|
||||
"""Issue a DB command.
|
||||
warnings.warn("The '_command' method is deprecated. "
|
||||
"Please use 'command' instead.", DeprecationWarning)
|
||||
return self.command(command, check, allowable_errors, sock)
|
||||
|
||||
def command(self, command, check=True, allowable_errors=[], _sock=None):
|
||||
"""Issue a MongoDB command.
|
||||
|
||||
Send a command to the database and return the response.
|
||||
|
||||
:Parameters:
|
||||
- `command`: document representing the command to be issued
|
||||
- `check` (optional): check the response for errors, raising
|
||||
:class:`~pymongo.errors.OperationFailure` if there are any
|
||||
- `allowable_errors`: if `check` is ``True``, error messages in this
|
||||
list will be ignored by error-checking
|
||||
"""
|
||||
result = self["$cmd"].find_one(command, _sock=sock,
|
||||
result = self["$cmd"].find_one(command, _sock=_sock,
|
||||
_must_use_master=True,
|
||||
_is_command=True)
|
||||
|
||||
@ -237,7 +252,7 @@ class Database(object):
|
||||
if name not in self.collection_names():
|
||||
return
|
||||
|
||||
self._command({"drop": unicode(name)})
|
||||
self.command({"drop": unicode(name)})
|
||||
|
||||
def validate_collection(self, name_or_collection):
|
||||
"""Validate a collection.
|
||||
@ -253,7 +268,7 @@ class Database(object):
|
||||
raise TypeError("name_or_collection must be an instance of "
|
||||
"(Collection, str, unicode)")
|
||||
|
||||
result = self._command({"validate": unicode(name)})
|
||||
result = self.command({"validate": unicode(name)})
|
||||
|
||||
info = result["result"]
|
||||
if info.find("exception") != -1 or info.find("corrupt") != -1:
|
||||
@ -266,7 +281,7 @@ class Database(object):
|
||||
Returns one of (:data:`~pymongo.OFF`,
|
||||
:data:`~pymongo.SLOW_ONLY`, :data:`~pymongo.ALL`).
|
||||
"""
|
||||
result = self._command({"profile": -1})
|
||||
result = self.command({"profile": -1})
|
||||
|
||||
assert result["was"] >= 0 and result["was"] <= 2
|
||||
return result["was"]
|
||||
@ -284,7 +299,7 @@ class Database(object):
|
||||
if not isinstance(level, types.IntType) or level < 0 or level > 2:
|
||||
raise ValueError("level must be one of (OFF, SLOW_ONLY, ALL)")
|
||||
|
||||
self._command({"profile": level})
|
||||
self.command({"profile": level})
|
||||
|
||||
def profiling_info(self):
|
||||
"""Returns a list containing current profiling information.
|
||||
@ -297,7 +312,7 @@ class Database(object):
|
||||
Return None if the last operation was error-free. Otherwise return the
|
||||
error that occurred.
|
||||
"""
|
||||
error = self._command({"getlasterror": 1})
|
||||
error = self.command({"getlasterror": 1})
|
||||
if error.get("err", 0) is None:
|
||||
return None
|
||||
if error["err"] == "not master":
|
||||
@ -309,7 +324,7 @@ class Database(object):
|
||||
|
||||
Returns a SON object with status information.
|
||||
"""
|
||||
return self._command({"getlasterror": 1})
|
||||
return self.command({"getlasterror": 1})
|
||||
|
||||
def previous_error(self):
|
||||
"""Get the most recent error to have occurred on this database.
|
||||
@ -318,7 +333,7 @@ class Database(object):
|
||||
`Database.reset_error_history`. Returns None if no such errors have
|
||||
occurred.
|
||||
"""
|
||||
error = self._command({"getpreverror": 1})
|
||||
error = self.command({"getpreverror": 1})
|
||||
if error.get("err", 0) is None:
|
||||
return None
|
||||
return error
|
||||
@ -329,7 +344,7 @@ class Database(object):
|
||||
Calls to `Database.previous_error` will only return errors that have
|
||||
occurred since the most recent call to this method.
|
||||
"""
|
||||
self._command({"reseterror": 1})
|
||||
self.command({"reseterror": 1})
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
@ -370,17 +385,17 @@ class Database(object):
|
||||
if not isinstance(password, types.StringTypes):
|
||||
raise TypeError("password must be an instance of (str, unicode)")
|
||||
|
||||
result = self._command({"getnonce": 1})
|
||||
result = self.command({"getnonce": 1})
|
||||
nonce = result["nonce"]
|
||||
digest = self._password_digest(name, password)
|
||||
md5hash = _md5func()
|
||||
md5hash.update("%s%s%s" % (nonce, unicode(name), digest))
|
||||
key = unicode(md5hash.hexdigest())
|
||||
try:
|
||||
result = self._command(SON([("authenticate", 1),
|
||||
("user", unicode(name)),
|
||||
("nonce", nonce),
|
||||
("key", key)]))
|
||||
result = self.command(SON([("authenticate", 1),
|
||||
("user", unicode(name)),
|
||||
("nonce", nonce),
|
||||
("key", key)]))
|
||||
return True
|
||||
except OperationFailure:
|
||||
return False
|
||||
@ -390,7 +405,7 @@ class Database(object):
|
||||
|
||||
Note that other databases may still be authorized.
|
||||
"""
|
||||
self._command({"logout": 1})
|
||||
self.command({"logout": 1})
|
||||
|
||||
def dereference(self, dbref):
|
||||
"""Dereference a DBRef, getting the SON object it points to.
|
||||
@ -433,7 +448,7 @@ class Database(object):
|
||||
code = Code(code)
|
||||
|
||||
command = SON([("$eval", code), ("args", list(args))])
|
||||
result = self._command(command)
|
||||
result = self.command(command)
|
||||
return result.get("retval", None)
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
|
||||
@ -318,34 +318,34 @@ class TestCursor(unittest.TestCase):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
|
||||
client_cursors = db._command({"cursorInfo": 1})["clientCursors_size"]
|
||||
by_location = db._command({"cursorInfo": 1})["byLocation_size"]
|
||||
client_cursors = db.command({"cursorInfo": 1})["clientCursors_size"]
|
||||
by_location = db.command({"cursorInfo": 1})["byLocation_size"]
|
||||
|
||||
test = db.test
|
||||
for i in range(10000):
|
||||
test.insert({"i": i})
|
||||
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
for _ in range(10):
|
||||
db.test.find_one()
|
||||
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
for _ in range(10):
|
||||
for x in db.test.find():
|
||||
break
|
||||
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
a = db.test.find()
|
||||
for x in a:
|
||||
@ -353,25 +353,25 @@ class TestCursor(unittest.TestCase):
|
||||
|
||||
self.assertNotEqual(
|
||||
client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertNotEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
del a
|
||||
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
a = db.test.find().limit(10)
|
||||
for x in a:
|
||||
break
|
||||
|
||||
self.assertEqual(client_cursors,
|
||||
db._command({"cursorInfo": 1})["clientCursors_size"])
|
||||
db.command({"cursorInfo": 1})["clientCursors_size"])
|
||||
self.assertEqual(by_location,
|
||||
db._command({"cursorInfo": 1})["byLocation_size"])
|
||||
db.command({"cursorInfo": 1})["byLocation_size"])
|
||||
|
||||
def test_rewind(self):
|
||||
self.db.test.save({"x": 1})
|
||||
|
||||
@ -182,11 +182,11 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(None, db.error())
|
||||
self.assertEqual(None, db.previous_error())
|
||||
|
||||
db._command({"forceerror": 1}, check=False)
|
||||
db.command({"forceerror": 1}, check=False)
|
||||
self.assert_(db.error())
|
||||
self.assert_(db.previous_error())
|
||||
|
||||
db._command({"forceerror": 1}, check=False)
|
||||
db.command({"forceerror": 1}, check=False)
|
||||
self.assert_(db.error())
|
||||
prev_error = db.previous_error()
|
||||
self.assertEqual(prev_error["nPrev"], 1)
|
||||
|
||||
@ -202,11 +202,11 @@ class TestMasterSlaveConnection(unittest.TestCase):
|
||||
|
||||
def cursor_count():
|
||||
count = 0
|
||||
res = self.connection.master.test_pymongo._command({
|
||||
res = self.connection.master.test_pymongo.command({
|
||||
"cursorInfo": 1})
|
||||
count += res["clientCursors_size"]
|
||||
for slave in self.connection.slaves:
|
||||
res = slave.test_pymongo._command({"cursorInfo": 1})
|
||||
res = slave.test_pymongo.command({"cursorInfo": 1})
|
||||
count += res["clientCursors_size"]
|
||||
return count
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user