class Mongo::DBRef

Represents a DBRef document in the database.

@since 2.1.0

Constants

COLLECTION

The constant for the collection reference field.

@since 2.1.0

DATABASE

The constant for the database field.

@since 2.1.0

ID

The constant for the id field.

@since 2.1.0

Attributes

collection[R]

@return [ String ] collection The collection name.

database[R]

@return [ String ] database The database name.

id[R]

@return [ BSON::ObjectId ] id The referenced document id.

Public Class Methods

new(collection, id, database = nil) click to toggle source

Instantiate a new DBRef.

@example Create the DBRef.

Mongo::DBRef.new('users', id, 'database')

@param [ String ] collection The collection name. @param [ BSON::ObjectId ] id The object id. @param [ String ] database The database name.

@since 2.1.0

# File lib/mongo/dbref.rb, line 71
def initialize(collection, id, database = nil)
  @collection = collection
  @id = id
  @database = database
end

Public Instance Methods

as_json(*args) click to toggle source

Get the DBRef as a JSON document

@example Get the DBRef as a JSON hash.

dbref.as_json

@return [ Hash ] The max key as a JSON hash.

@since 2.1.0

# File lib/mongo/dbref.rb, line 55
def as_json(*args)
  document = { COLLECTION => collection, ID => id }
  document.merge!(DATABASE => database) if database
  document
end
to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) click to toggle source

Converts the DBRef to raw BSON.

@example Convert the DBRef to raw BSON.

dbref.to_bson

@param [ BSON::ByteBuffer ] buffer The encoded BSON buffer to append to. @param [ true, false ] validating_keys Whether keys should be validated when serializing.

@return [ String ] The raw BSON.

@since 2.1.0

# File lib/mongo/dbref.rb, line 88
def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
  as_json.to_bson(buffer)
end