def __init__(self, connection, name): """Get a database by connection and name. Raises :class:`TypeError` if `name` is not an instance of :class:`basestring`. Raises :class:`~pymongo.errors.InvalidName` if `name` is not a valid database name. :Parameters: - `connection`: a :class:`~pymongo.connection.Connection` instance - `name`: database name .. mongodoc:: databases """ if not isinstance(name, basestring): raise TypeError("name must be an instance of basestring") self.__check_name(name) self.__name = unicode(name) self.__connection = connection # TODO remove the callable_value wrappers after deprecation is complete self.__name_w = helpers.callable_value(self.__name, "Database.name") self.__connection_w = helpers.callable_value(self.__connection, "Database.connection") self.__incoming_manipulators = [] self.__incoming_copying_manipulators = [] self.__outgoing_manipulators = [] self.__outgoing_copying_manipulators = [] self.add_son_manipulator(ObjectIdInjector()) self.__system_js = SystemJS(self)
def __init__(self, database, name, options=None): """Get / create a Mongo collection. Raises :class:`TypeError` if `name` is not an instance of :class:`basestring`. Raises :class:`~pymongo.errors.InvalidName` if `name` is not a valid collection name. Raises :class:`TypeError` if `options` is not an instance of :class:`dict`. If `options` is non-empty a create command will be sent to the database. Otherwise the collection will be created implicitly on first use. :Parameters: - `database`: the database to get a collection from - `name`: the name of the collection to get - `options`: dictionary of collection options. see :meth:`~pymongo.database.Database.create_collection` for details. .. mongodoc:: collections """ if not isinstance(name, basestring): raise TypeError("name must be an instance of basestring") if options is not None and not isinstance(options, dict): raise TypeError("options must be an instance of dict") if not name or ".." in name: raise InvalidName("collection names cannot be empty") if "$" in name and not (name.startswith("oplog.$main") or name.startswith("$cmd")): raise InvalidName("collection names must not " "contain '$': %r" % name) if name[0] == "." or name[-1] == ".": raise InvalidName("collecion names must not start " "or end with '.': %r" % name) if "\x00" in name: raise InvalidName("collection names must not contain the " "null character") self.__database = database self.__name = unicode(name) self.__full_name = u"%s.%s" % (self.__database.name, self.__name) # TODO remove the callable_value wrappers after deprecation is complete self.__database_w = helpers.callable_value(self.__database, "Collection.database") self.__name_w = helpers.callable_value(self.__name, "Collection.name") self.__full_name_w = helpers.callable_value(self.__full_name, "Collection.full_name") if options is not None: self.__create(options)
def port(self): """Current connected port. .. versionchanged:: 1.3 ``port`` is now a property rather than a method. The ``port()`` method is deprecated. """ return helpers.callable_value(self.__port, "Connection.port")
def __init__(self, database, name, options=None, create=False, **kwargs): """Get / create a Mongo collection. Raises :class:`TypeError` if `name` is not an instance of :class:`basestring`. Raises :class:`~pymongo.errors.InvalidName` if `name` is not a valid collection name. Any additional keyword arguments will be used as options passed to the create command. See :meth:`~pymongo.database.Database.create_collection` for valid options. If `create` is ``True`` or additional keyword arguments are present a create command will be sent. Otherwise, a create command will not be sent and the collection will be created implicitly on first use. :Parameters: - `database`: the database to get a collection from - `name`: the name of the collection to get - `options`: DEPRECATED dictionary of collection options - `create` (optional): if ``True``, force collection creation even without options being set - `**kwargs` (optional): additional keyword arguments will be passed as options for the create collection command .. versionchanged:: 1.5 deprecating `options` in favor of kwargs .. versionadded:: 1.5 the `create` parameter .. mongodoc:: collections """ if not isinstance(name, basestring): raise TypeError("name must be an instance of basestring") if options is not None: warnings.warn("the options argument to Collection is deprecated " "and will be removed. please use kwargs instead.", DeprecationWarning) if not isinstance(options, dict): raise TypeError("options must be an instance of dict") options.update(kwargs) elif kwargs: options = kwargs if not name or ".." in name: raise InvalidName("collection names cannot be empty") if "$" in name and not (name.startswith("oplog.$main") or name.startswith("$cmd")): raise InvalidName("collection names must not " "contain '$': %r" % name) if name[0] == "." or name[-1] == ".": raise InvalidName("collecion names must not start " "or end with '.': %r" % name) if "\x00" in name: raise InvalidName("collection names must not contain the " "null character") self.__database = database self.__name = unicode(name) self.__full_name = u"%s.%s" % (self.__database.name, self.__name) # TODO remove the callable_value wrappers after deprecation is complete self.__database_w = helpers.callable_value(self.__database, "Collection.database") self.__name_w = helpers.callable_value(self.__name, "Collection.name") self.__full_name_w = helpers.callable_value(self.__full_name, "Collection.full_name") if create or options is not None: self.__create(options)