Method: Mongo::Collection#initialize

Defined in:
lib/jmongo/collection.rb

#initialize(*args) ⇒ Collection

Initialize a collection object.

db, name, options=nil, j_collection=nil

Parameters:

  • db (DB)

    a MongoDB database instance.

  • name (String, Symbol)

    the name of the collection.

Raises:

  • (InvalidNSName)

    if collection name is empty, contains ‘$’, or starts or ends with ‘.’

  • (TypeError)

    if collection name is not a string or symbol



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jmongo/collection.rb', line 39

def initialize(*args)
  j_collection = nil
  @opts = {}
  if args.size == 4
    j_collection = args.pop
  end
  if args.size == 3
    @opts = args.pop
  end
  if args.size < 2
    raise ArgumentError.new("Must supply at least name and db parameters")
  end
  if args.first.is_a?(String)
    name, db = args
  else
    db, name = args
  end
  @name = validate_name(name)
  @db, @j_db  = db, db.j_db
  @connection = @db.connection
  @pk_factory = @opts.delete(:pk)|| BSON::ObjectId
  @hint = nil

  @monitor = @opts.delete(:monitor)
  if @monitor
    setup_monitor
  elsif @opts.has_key?(:monitor_source)
    @monitor_source = @opts.delete(:monitor_source)
  end

  @j_collection = j_collection || @j_db.create_collection(@name, to_dbobject(@opts))
end