Class: Mongo::DBRef

Inherits:
Object
  • Object
show all
Includes:
BSON::JSON
Defined in:
build/ruby-driver-master/lib/mongo/dbref.rb

Overview

Represents a DBRef document in the database.

Since:

  • 2.1.0

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

COLLECTION =

The constant for the collection reference field.

Since:

  • 2.1.0

'$ref'.freeze
ID =

The constant for the id field.

Since:

  • 2.1.0

'$id'.freeze
DATABASE =

The constant for the database field.

Since:

  • 2.1.0

'$db'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, id, database = nil) ⇒ DBRef

Instantiate a new DBRef.

Examples:

Create the DBRef.

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

Parameters:

  • collection (String)

    The collection name.

  • id (BSON::ObjectId)

    The object id.

  • database (String) (defaults to: nil)

    The database name.

Since:

  • 2.1.0



74
75
76
77
78
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 74

def initialize(collection, id, database = nil)
  @collection = collection
  @id = id
  @database = database
end

Instance Attribute Details

#collectionString (readonly)

Returns collection The collection name.

Returns:

  • (String)

    collection The collection name.

Since:

  • 2.1.0



42
43
44
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 42

def collection
  @collection
end

#databaseString (readonly)

Returns database The database name.

Returns:

  • (String)

    database The database name.

Since:

  • 2.1.0



48
49
50
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 48

def database
  @database
end

#idBSON::ObjectId (readonly)

Returns id The referenced document id.

Returns:

  • (BSON::ObjectId)

    id The referenced document id.

Since:

  • 2.1.0



45
46
47
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 45

def id
  @id
end

Instance Method Details

#as_json(*args) ⇒ Hash

Get the DBRef as a JSON document

Examples:

Get the DBRef as a JSON hash.

dbref.as_json

Returns:

  • (Hash)

    The max key as a JSON hash.

Since:

  • 2.1.0



58
59
60
61
62
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 58

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?) ⇒ String

Converts the DBRef to raw BSON.

Examples:

Convert the DBRef to raw BSON.

dbref.to_bson

Parameters:

  • buffer (BSON::ByteBuffer) (defaults to: BSON::ByteBuffer.new)

    The encoded BSON buffer to append to.

  • validating_keys (true, false) (defaults to: BSON::Config.validating_keys?)

    Whether keys should be validated when serializing.

Returns:

  • (String)

    The raw BSON.

Since:

  • 2.1.0



91
92
93
# File 'build/ruby-driver-master/lib/mongo/dbref.rb', line 91

def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
  as_json.to_bson(buffer)
end