OpenQuizz
Une application de gestion des contenus pédagogiques
ObjectId Class Reference
Inheritance diagram for ObjectId:
Collaboration diagram for ObjectId:

Public Member Functions

def __init__ (self, oid=None)
 
def from_datetime (cls, generation_time)
 
def is_valid (cls, oid)
 
def binary (self)
 
def generation_time (self)
 
def __getstate__ (self)
 
def __setstate__ (self, value)
 
def __str__ (self)
 
def __repr__ (self)
 
def __eq__ (self, other)
 
def __ne__ (self, other)
 
def __lt__ (self, other)
 
def __le__ (self, other)
 
def __gt__ (self, other)
 
def __ge__ (self, other)
 
def __hash__ (self)
 

Detailed Description

A MongoDB ObjectId.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  oid = None 
)
Initialize a new ObjectId.

An ObjectId is a 12-byte unique identifier consisting of:

  - a 4-byte value representing the seconds since the Unix epoch,
  - a 5-byte random value,
  - a 3-byte counter, starting with a random value.

By default, ``ObjectId()`` creates a new unique identifier. The
optional parameter `oid` can be an :class:`ObjectId`, or any 12
:class:`bytes` or, in Python 2, any 12-character :class:`str`.

For example, the 12 bytes b'foo-bar-quux' do not follow the ObjectId
specification but they are acceptable input::

  >>> ObjectId(b'foo-bar-quux')
  ObjectId('666f6f2d6261722d71757578')

`oid` can also be a :class:`unicode` or :class:`str` of 24 hex digits::

  >>> ObjectId('0123456789ab0123456789ab')
  ObjectId('0123456789ab0123456789ab')
  >>>
  >>> # A u-prefixed unicode literal:
  >>> ObjectId(u'0123456789ab0123456789ab')
  ObjectId('0123456789ab0123456789ab')

Raises :class:`~bson.errors.InvalidId` if `oid` is not 12 bytes nor
24 hex digits, or :class:`TypeError` if `oid` is not an accepted type.

:Parameters:
  - `oid` (optional): a valid ObjectId.

.. mongodoc:: objectids

.. versionchanged:: 3.8
   :class:`~bson.objectid.ObjectId` now implements the `ObjectID
   specification version 0.2
   <https://github.com/mongodb/specifications/blob/master/source/
   objectid.rst>`_.

Member Function Documentation

◆ __eq__()

def __eq__ (   self,
  other 
)

◆ __ge__()

def __ge__ (   self,
  other 
)

◆ __getstate__()

def __getstate__ (   self)
return value of object for pickling.
needed explicitly because __slots__() defined.

◆ __gt__()

def __gt__ (   self,
  other 
)

◆ __hash__()

def __hash__ (   self)
Get a hash value for this :class:`ObjectId`.

◆ __le__()

def __le__ (   self,
  other 
)

◆ __lt__()

def __lt__ (   self,
  other 
)

◆ __ne__()

def __ne__ (   self,
  other 
)

◆ __repr__()

def __repr__ (   self)

◆ __setstate__()

def __setstate__ (   self,
  value 
)
explicit state set from pickling

◆ __str__()

def __str__ (   self)

◆ binary()

def binary (   self)
12-byte binary representation of this ObjectId.

◆ from_datetime()

def from_datetime (   cls,
  generation_time 
)
Create a dummy ObjectId instance with a specific generation time.

This method is useful for doing range queries on a field
containing :class:`ObjectId` instances.

.. warning::
   It is not safe to insert a document containing an ObjectId
   generated using this method. This method deliberately
   eliminates the uniqueness guarantee that ObjectIds
   generally provide. ObjectIds generated with this method
   should be used exclusively in queries.

`generation_time` will be converted to UTC. Naive datetime
instances will be treated as though they already contain UTC.

An example using this helper to get documents where ``"_id"``
was generated before January 1, 2010 would be:

>>> gen_time = datetime.datetime(2010, 1, 1)
>>> dummy_id = ObjectId.from_datetime(gen_time)
>>> result = collection.find({"_id": {"$lt": dummy_id}})

:Parameters:
  - `generation_time`: :class:`~datetime.datetime` to be used
    as the generation time for the resulting ObjectId.

◆ generation_time()

def generation_time (   self)
A :class:`datetime.datetime` instance representing the time of
generation for this :class:`ObjectId`.

The :class:`datetime.datetime` is timezone aware, and
represents the generation time in UTC. It is precise to the
second.

◆ is_valid()

def is_valid (   cls,
  oid 
)
Checks if a `oid` string is valid or not.

:Parameters:
  - `oid`: the object id to validate

.. versionadded:: 2.3

The documentation for this class was generated from the following file: