mongodb-mongo 0.5.3 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -3,10 +3,6 @@
3
3
  This is a Ruby driver for the 10gen Mongo DB. For more information about
4
4
  Mongo, see http://www.mongodb.org.
5
5
 
6
- Note: this driver is still alpha quality. The API will change, as *may* the
7
- data saved to the database (especially primary key values). Do *_not_* use
8
- this for any production data yet.
9
-
10
6
  Start by reading the XGen::Mongo::Driver::Mongo and XGen::Mongo::Driver::DB
11
7
  documentation, then move on to XGen::Mongo::Driver::Collection and
12
8
  XGen::Mongo::Driver::Cursor.
@@ -35,7 +31,7 @@ a separate project.
35
31
  Install the "mongo" gem by typing
36
32
 
37
33
  $ gem sources -a http://gems.github.com
38
- $ sudo gem install mongodb-mongo-ruby-driver
34
+ $ sudo gem install mongodb-mongo
39
35
 
40
36
  The first line tells RubyGems to add the GitHub gem repository. You only need
41
37
  to run this command once.
@@ -49,6 +45,9 @@ you can install it as a gem from the source by typing
49
45
 
50
46
  $ rake gem:install
51
47
 
48
+ Note: when you install the gem this way it is called "mongo", not
49
+ "mongodb-mongo". In either case, you "require 'mongo'" in your source code.
50
+
52
51
 
53
52
  = Examples
54
53
 
@@ -89,7 +88,8 @@ Here is some simple example code:
89
88
 
90
89
  The GridStore class is a Ruby implementation of Mongo's GridFS file storage
91
90
  system. An instance of GridStore is like an IO object. See the rdocs for
92
- details.
91
+ details, and see examples/gridfs.rb for code that uses many of the GridStore
92
+ features like metadata, content type, rewind/seek/tell, etc.
93
93
 
94
94
  Note that the GridStore class is not automatically required when you require
95
95
  'mongo'. You need to require 'mongo/gridfs'.
data/bin/run_test_script CHANGED
@@ -5,6 +5,10 @@
5
5
 
6
6
  HERE=`dirname $0`
7
7
 
8
+ if [ ! -f $HERE/../tests/mongo-qa/$1 ] ; then
9
+ exit 0
10
+ fi
11
+
8
12
  begintime=`date`
9
13
  ruby $HERE/../tests/mongo-qa/$1 >> $2
10
14
  exitval=$?
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Note: Ruby 1.9 is faster than 1.8, as expected.
4
+
5
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
6
+ require 'mongo'
7
+
8
+ include XGen::Mongo::Driver
9
+
10
+ N = 30_000
11
+
12
+ def report(str, t0, t1, n)
13
+ dt = t1.to_f - t0.to_f
14
+ printf("%16s: %03.8f\n", str, dt)
15
+ end
16
+
17
+ def benchmark(str, n, db, after_proc=nil)
18
+ coll = db.collection('benchmark')
19
+ t0 = Time.new
20
+ n.times { |i| yield coll, i }
21
+ after_proc.call if after_proc
22
+ report(str, t0, Time.new, n)
23
+ end
24
+
25
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
26
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
27
+
28
+ db = Mongo.new(host, port).db('ruby-benchmark')
29
+ db.drop_collection('benchmark')
30
+ coll = db.collection('benchmark')
31
+
32
+ coll.create_index('foo', 'i')
33
+
34
+ # Call to db.error forces inserts to finish
35
+ benchmark('insert', N, db, Proc.new{db.error}) { |coll, i|
36
+ coll.insert('i' => i)
37
+ }
38
+ benchmark('find_first', N, db) { |coll, i|
39
+ coll.find_first
40
+ }
41
+ benchmark('find', N, db) { |coll, i|
42
+ coll.find('i' => 3).each { }
43
+ coll.find('i' => 234).each { }
44
+ coll.find('i' => 9876).each { }
45
+ }
46
+ benchmark('find gt/lt', N, db) { |coll, i|
47
+ h = {'i' => {'$gt' => 200, '$lt' => 200}}
48
+ coll.find(h).each {}
49
+ }
data/examples/admin.rb ADDED
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include XGen::Mongo::Driver
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.create_collection('test')
13
+
14
+ # Erase all records from collection, if any
15
+ coll.clear
16
+
17
+ admin = db.admin
18
+
19
+ # Profiling level set/get
20
+ p admin.profiling_level
21
+
22
+ # Start profiling everything
23
+ admin.profiling_level = :all
24
+
25
+ # Read records, creating a profiling event
26
+ coll.find().to_a
27
+
28
+ # Stop profiling
29
+ admin.profiling_level = :off
30
+
31
+ # Print all profiling info
32
+ pp admin.profiling_info
33
+
34
+ # Validate returns a hash if all is well or raises an exception if there is a
35
+ # problem.
36
+ info = admin.validate_collection(coll.name)
37
+ puts "valid = #{info['ok']}"
38
+ puts info['result']
39
+
40
+ # Destroy the collection
41
+ coll.drop
@@ -1,4 +1,3 @@
1
- require "rubygems"
2
1
  require "benchmark"
3
2
 
4
3
  $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
@@ -17,7 +16,7 @@ TEST_COUNT = 100
17
16
 
18
17
  puts "Generating benchmark data"
19
18
  msgs = %w{hola hello aloha ciao}
20
- arr = OBJS_COUNT.times.map {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
19
+ arr = (0..OBJS_COUNT).collect {|x| { :number => x, :rndm => (rand(5)+1), :msg => msgs[rand(4)] }}
21
20
 
22
21
  puts "Running benchmark"
23
22
  Benchmark.bmbm do |results|
@@ -33,6 +32,11 @@ Benchmark.bmbm do |results|
33
32
  coll.insert(arr)
34
33
  }
35
34
  }
35
+ results.report("find_first: ") {
36
+ TEST_COUNT.times {
37
+ coll.find_first(:number => 0)
38
+ }
39
+ }
36
40
  end
37
41
 
38
42
  coll.clear
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+
4
+ include XGen::Mongo::Driver
5
+
6
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
8
+
9
+ puts "Connecting to #{host}:#{port}"
10
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
11
+ db.drop_collection('test')
12
+
13
+ # A capped collection has a max size and optionally a max number of records.
14
+ # Old records get pushed out by new ones once the size or max num records is
15
+ # reached.
16
+ coll = db.create_collection('test', :capped => true, :size => 1024, :max => 12)
17
+
18
+ 100.times { |i| coll.insert('a' => i+1) }
19
+
20
+ # We will only see the last 12 records
21
+ coll.find().each { |row| p row }
22
+
23
+ coll.drop
@@ -0,0 +1,47 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include XGen::Mongo::Driver
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.collection('test')
13
+
14
+ # Erase all records from collection, if any
15
+ coll.clear
16
+
17
+ # Insert 3 records
18
+ 3.times { |i| coll.insert({'a' => i+1}) }
19
+
20
+ # Cursors don't run their queries until you actually attempt to retrieve data
21
+ # from them.
22
+
23
+ # Find returns a Cursor, which is Enumerable. You can iterate:
24
+ coll.find().each { |row| pp row }
25
+
26
+ # You can turn it into an array
27
+ array = coll.find().to_a
28
+
29
+ # You can iterate after turning it into an array (the cursor will iterate over
30
+ # the copy of the array that it saves internally.)
31
+ cursor = coll.find()
32
+ array = cursor.to_a
33
+ cursor.each { |row| pp row }
34
+
35
+ # You can get the next object
36
+ first_object = coll.find().next_object
37
+
38
+ # next_object returns nil if there are no more objects that match
39
+ cursor = coll.find()
40
+ obj = cursor.next_object
41
+ while obj
42
+ pp obj
43
+ obj = cursor.next_object
44
+ end
45
+
46
+ # Destroy the collection
47
+ coll.drop
@@ -0,0 +1,87 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'mongo/gridfs'
4
+
5
+ include XGen::Mongo::Driver
6
+ include XGen::Mongo::GridFS
7
+
8
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
9
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
10
+
11
+ puts "Connecting to #{host}:#{port}"
12
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
13
+
14
+ def dump(db, fname)
15
+ GridStore.open(db, fname, 'r') { |f| puts f.read }
16
+ end
17
+
18
+ # Write a new file
19
+ GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
20
+
21
+ # Read it and print out the contents
22
+ dump(db, 'foobar') # defined above
23
+
24
+ # Append more data
25
+ GridStore.open(db, 'foobar', 'w+') { |f| f.write("\n"); f.puts "line two" }
26
+ dump(db, 'foobar')
27
+
28
+ # Overwrite
29
+ GridStore.open(db, 'foobar', 'w') { |f| f.puts "hello, sailor!" }
30
+ dump(db, 'foobar')
31
+
32
+ # File existence tests
33
+ puts "File 'foobar' exists: #{GridStore.exist?(db, 'foobar')}"
34
+ puts "File 'does-not-exist' exists: #{GridStore.exist?(db, 'does-not-exist')}"
35
+
36
+ # Read with offset (uses seek)
37
+ puts GridStore.read(db, 'foobar', 6, 7)
38
+
39
+ # Rewind/seek/tell
40
+ GridStore.open(db, 'foobar', 'w') { |f|
41
+ f.write "hello, world!"
42
+ f.rewind
43
+ f.write "xyzzz"
44
+ puts f.tell # => 5
45
+ f.seek(4)
46
+ f.write('y')
47
+ }
48
+ dump(db, 'foobar') # => 'xyzzy'
49
+
50
+ # Unlink (delete)
51
+ GridStore.unlink(db, 'foobar')
52
+ puts "File 'foobar' exists after delete: #{GridStore.exist?(db, 'foobar')}"
53
+
54
+ # Metadata
55
+ GridStore.open(db, 'foobar', 'w') { |f| f.write("hello, world!") }
56
+ GridStore.open(db, 'foobar', 'r') { |f|
57
+ puts f.content_type
58
+ puts f.upload_date
59
+ puts f.chunk_size
60
+ puts f.metadata.inspect
61
+ }
62
+
63
+ # Add some metadata; change content type
64
+ GridStore.open(db, 'foobar', 'w+') { |f|
65
+ f.content_type = 'text/xml'
66
+ f.metadata = {'a' => 1}
67
+ }
68
+ # Print it
69
+ GridStore.open(db, 'foobar', 'r') { |f|
70
+ puts f.content_type
71
+ puts f.upload_date
72
+ puts f.chunk_size
73
+ puts f.metadata.inspect
74
+ }
75
+
76
+ # You can also set metadata when initially writing the file. Setting :root
77
+ # means that the file and its chunks are stored in a different root
78
+ # collection: instead of gridfs.files and gridfs.chunks, here we use
79
+ # my_files.files and my_files.chunks.
80
+ GridStore.open(db, 'foobar', 'w',
81
+ :content_type => 'text/plain',
82
+ :metadata => {'a' => 1},
83
+ :chunk_size => 1024 * 4,
84
+ :root => 'my_files') { |f|
85
+ f.puts 'hello, world'
86
+ }
87
+
data/examples/info.rb ADDED
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+
4
+ include XGen::Mongo::Driver
5
+
6
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
8
+
9
+ puts "Connecting to #{host}:#{port}"
10
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
11
+ coll = db.collection('test')
12
+
13
+ # Erase all records from collection, if any
14
+ coll.clear
15
+
16
+ # Insert 3 records
17
+ 3.times { |i| coll.insert({'a' => i+1}) }
18
+
19
+ # Collection names in database
20
+ p db.collection_names
21
+
22
+ # More information about each collection
23
+ p db.collections_info
24
+
25
+ # Index information
26
+ db.create_index('test', 'index_name', ['a'])
27
+ p db.index_information('test')
28
+
29
+ # Destroy the collection
30
+ coll.drop
@@ -0,0 +1,69 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+ require 'pp'
4
+
5
+ include XGen::Mongo::Driver
6
+
7
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
8
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
9
+
10
+ puts "Connecting to #{host}:#{port}"
11
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
12
+ coll = db.collection('test')
13
+
14
+ # Remove all records, if any
15
+ coll.clear
16
+
17
+ # Insert three records
18
+ coll.insert('a' => 1)
19
+ coll.insert('a' => 2)
20
+ coll.insert('b' => 3)
21
+
22
+ # Count.
23
+ puts "There are #{coll.count()} records."
24
+
25
+ # Find all records. find() returns a Cursor.
26
+ cursor = coll.find()
27
+
28
+ # Print them. Note that all records have an _id automatically added by the
29
+ # database. See pk.rb for an example of how to use a primary key factory to
30
+ # generate your own values for _id.
31
+ cursor.each { |row| pp row }
32
+
33
+ # Cursor has a to_a method that slurps all records into memory.
34
+ rows = coll.find().to_a
35
+ rows.each { |row| pp row }
36
+
37
+ # See Collection#find. From now on in this file, we won't be printing the
38
+ # records we find.
39
+ coll.find('a' => 1)
40
+
41
+ # Find records sort by 'a', offset 1, limit 2 records.
42
+ # Sort can be single name, array, or hash.
43
+ coll.find({}, {:offset => 1, :limit => 2, :sort => 'a'})
44
+
45
+ # Find all records with 'a' > 1. There is also $lt, $gte, and $lte.
46
+ coll.find({'a' => {'$gt' => 1}})
47
+ coll.find({'a' => {'$gt' => 1, '$lte' => 3}})
48
+
49
+ # Find all records with 'a' in a set of values.
50
+ coll.find('a' => {'$in' => [1,2]})
51
+
52
+ # Find by regexp
53
+ coll.find('a' => /[1|2]/)
54
+
55
+ # Print query explanation
56
+ pp coll.find('a' => /[1|2]/).explain()
57
+
58
+ # Use a hint with a query. Need an index. Hints can be stored with the
59
+ # collection, in which case they will be used with all queries, or they can be
60
+ # specified per query, in which case that hint overrides the hint associated
61
+ # with the collection if any.
62
+ coll.create_index('test_a_index', 'a')
63
+ coll.hint = 'a'
64
+
65
+ # You will see a different explanation now that the hint is in place
66
+ pp coll.find('a' => /[1|2]/).explain()
67
+
68
+ # Override hint for single query
69
+ coll.find({'a' => 1}, :hint => 'b')
@@ -0,0 +1,34 @@
1
+ $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mongo'
3
+
4
+ include XGen::Mongo::Driver
5
+
6
+ host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
7
+ port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT
8
+
9
+ puts "Connecting to #{host}:#{port}"
10
+ db = Mongo.new(host, port).db('ruby-mongo-examples')
11
+
12
+ db.drop_collection('does-not-exist')
13
+ db.create_collection('test')
14
+
15
+ db.strict = true
16
+
17
+ begin
18
+ # Can't reference collection that does not exist
19
+ db.collection('does-not-exist')
20
+ puts "error: expected exception"
21
+ rescue => ex
22
+ puts "expected exception: #{ex}"
23
+ end
24
+
25
+ begin
26
+ # Can't create collection that already exists
27
+ db.create_collection('test')
28
+ puts "error: expected exception"
29
+ rescue => ex
30
+ puts "expected exception: #{ex}"
31
+ end
32
+
33
+ db.strict = false
34
+ db.drop_collection('test')