Class BSON::ObjectId
In: lib/bson/types/object_id.rb
Parent: Object

Generates MongoDB object ids.

@core objectids

Methods

==   as_json   create_pk   eql?   from_string   from_time   generation_time   hash   inspect   legal?   new   to_a   to_json   to_s  

Attributes

data  [RW] 

Public Class methods

Adds a primary key to the given document if needed.

@param [Hash] doc a document requiring an _id.

@return [Mongo::ObjectId, Object] returns a newly-created or

  current _id for the given document.

Given a string representation of an ObjectId, return a new ObjectId with that value.

@param [String] str

@return [Mongo::ObjectId]

Create an object id from the given time. This is useful for doing range queries; it works because MongoDB‘s object ids begin with a timestamp.

@param [Time] time a utc time to encode as an object id.

@option opts [:unique] (false) If false, the object id‘s bytes

  succeeding the timestamp will be zeroed; if true, they'll
  consist of the standard machine id, pid, and counter.

@return [Mongo::ObjectId]

@example Return all document created before Jan 1, 2010.

  time = Time.utc(2010, 1, 1)
  time_id = ObjectId.from_time(time)
  collection.find({'_id' => {'$lt' => time_id}})

Determine if the supplied string is legal. Legal strings will consist of 24 hexadecimal characters.

@param [String] str

@return [Boolean]

Create a new object id. If no parameter is given, an id corresponding to the ObjectId BSON data type will be created. This is a 12-byte value consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id, and a 3-byte counter.

@param [Array] data should be an array of bytes. If you want

  to generate a standard MongoDB object id, leave this argument blank.

@option opts :data (nil) An array of bytes to use as the object id. @option opts :time (nil) The value of this object ids timestamp. Note that

  the remaining bytes will consist of the standard machine id, pid, and counter. If
  you need a zeroed timestamp, used ObjectId.from_time.

Public Instance methods

==(object_id)

Alias for eql?

Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3 introduced as_json to create the needed hash structure to encode objects into JSON.

@return [Hash] the hash representation as MongoDB extended JSON

Check equality of this object id with another.

@param [BSON::ObjectId] object_id

Return the UTC time at which this ObjectId was generated. This may be used in lieu of a created_at timestamp since this information is always encoded in the object id.

@return [Time] the time at which this object was created.

Get a unique hashcode for this object. This is required since we‘ve defined an eql? method.

@return [Integer]

Get an array representation of the object id.

@return [Array]

Convert to MongoDB extended JSON format. Since JSON includes type information, but lacks an ObjectId type, this JSON format encodes the type using an $oid key.

@return [String] the object id represented as MongoDB extended JSON.

Get a string representation of this object id.

@return [String]

[Validate]