Skip to content

Commit 5c5ce7f

Browse files
authored
Separate authentication and monitoring docs & reorganize admin tasks (#1028)
* Separate authentication and monitoring docs * Move collections and preferences documentation * Rename admin tasks to database tasks
1 parent c754919 commit 5c5ce7f

7 files changed

+226
-248
lines changed

source/ruby-driver-tutorials.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ operations available in the Ruby driver.
1616
:titlesonly:
1717

1818
/tutorials/ruby-driver-create-client
19+
/tutorials/ruby-driver-authentication
1920
/tutorials/ruby-driver-crud-operations
2021
/tutorials/ruby-driver-collection-tasks
2122
/tutorials/ruby-driver-projections
2223
/tutorials/ruby-driver-sessions
2324
/tutorials/ruby-driver-transactions
2425
/tutorials/ruby-driver-change-streams
25-
/tutorials/ruby-driver-admin-tasks
26+
/tutorials/ruby-driver-database-tasks
27+
/tutorials/ruby-driver-monitoring
2628
/tutorials/ruby-driver-indexing
2729
/tutorials/ruby-driver-collations
2830
/tutorials/ruby-driver-aggregation
Lines changed: 1 addition & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
==============
2-
Administration
2+
Authentication
33
==============
44

55
.. default-domain:: mongodb
@@ -10,90 +10,6 @@ Administration
1010
:depth: 1
1111
:class: singlecol
1212

13-
Databases
14-
---------
15-
16-
The driver provides various helpers on database objects for executing
17-
commands, getting collection lists, and administrative tasks.
18-
19-
List Collections
20-
````````````````
21-
22-
To get a list of collections or collection names for a database, use
23-
``collections`` and ``collection_names``, respectively.
24-
25-
.. code-block:: ruby
26-
27-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
28-
database = client.database
29-
30-
database.collections # Returns an array of Collection objects.
31-
database.collection_names # Returns an array of collection names as strings.
32-
33-
To execute any command on the database, use the ``command`` method.
34-
35-
.. code-block:: ruby
36-
37-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
38-
database = client.database
39-
40-
result = database.command(:ismaster => 1)
41-
result.first # Returns the BSON::Document returned from the server.
42-
43-
Drop Database
44-
`````````````
45-
46-
To drop a database, use the ``drop`` method.
47-
48-
.. code-block:: ruby
49-
50-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
51-
client.database.drop
52-
53-
Collections
54-
-----------
55-
56-
The driver provides some helpers for administrative tasks with
57-
collections.
58-
59-
To create a collection with options (such as creating a capped collection),
60-
pass the options when getting the collection from the client, then call
61-
``create``.
62-
63-
.. code-block:: ruby
64-
65-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
66-
artists = client[:artists, :capped => true, :size => 1024]
67-
artists.create
68-
artists.capped? # Returns true.
69-
70-
Drop Collection
71-
```````````````
72-
73-
To drop a collection, call ``drop`` on the collection object.
74-
75-
.. code-block:: ruby
76-
77-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
78-
artists = client[:artists]
79-
artists.drop
80-
81-
Changing Read/Write Preferences
82-
```````````````````````````````
83-
84-
To change the default read preference or write concern for specific operations,
85-
use the ``with`` method on the collection.
86-
87-
.. code-block:: ruby
88-
89-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
90-
artists = client[:artists]
91-
artists.with(:read => { :mode => :primary_preferred }).find.to_a
92-
artists.with(:write => { :w => :3 }).insert_one( { :name => 'Depeche Mode' } )
93-
94-
Authentication
95-
--------------
96-
9713
MongoDB supports a variety of
9814
:manual:`authentication mechanisms </core/authentication/>`.
9915

@@ -258,119 +174,3 @@ authentication, see the :manual:`manual
258174
auth_mech: :gssapi,
259175
user: 'test',
260176
password: '123' )
261-
262-
Logger
263-
------
264-
265-
You can either use the default global driver logger or set your own. To set your own:
266-
267-
.. code-block:: ruby
268-
269-
Mongo::Logger.logger = other_logger
270-
271-
See the `Ruby Logger documentation <http://ruby-doc.org/stdlib-2.2.0/libdoc/logger/rdoc/Logger.html>`_
272-
for more information on the default logger API and available levels.
273-
274-
Changing the Logger Level
275-
`````````````````````````
276-
277-
To change the logger level:
278-
279-
.. code-block:: ruby
280-
281-
Mongo::Logger.logger.level = Logger::WARN
282-
283-
For more control, a logger can be passed to a client for per-client control over logging.
284-
285-
.. code-block:: ruby
286-
287-
my_logger = Logger.new($stdout)
288-
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :logger => my_logger )
289-
290-
Truncation
291-
``````````
292-
293-
The default logging truncates logs at 250 characters by default. To turn this off pass an
294-
option to the client instance.
295-
296-
.. code-block:: ruby
297-
298-
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :truncate_logs => false )
299-
300-
Monitoring
301-
----------
302-
303-
All user-initiated commands that are sent to the server publish events that can be
304-
subscribed to for fine grained information. The monitoring API publishes a guaranteed
305-
start event for each command, then either a succeeded or failed event. A subscriber
306-
must implement 3 methods: ``started``, ``succeeded``, and ``failed``, each which takes
307-
a single parameter for the event. An example is the default logging subscriber included
308-
in the driver:
309-
310-
.. code-block:: ruby
311-
312-
module Mongo
313-
class Monitoring
314-
class CommandLogSubscriber
315-
include Loggable
316-
317-
attr_reader :options
318-
319-
LOG_STRING_LIMIT = 250
320-
321-
def initialize(options = {})
322-
@options = options
323-
end
324-
325-
def started(event)
326-
log_debug("#{prefix(event)} | STARTED | #{format_command(event.command)}")
327-
end
328-
329-
def succeeded(event)
330-
log_debug("#{prefix(event)} | SUCCEEDED | #{event.duration}s")
331-
end
332-
333-
def failed(event)
334-
log_debug("#{prefix(event)} | FAILED | #{event.message} | #{event.duration}s")
335-
end
336-
337-
private
338-
339-
def format_command(args)
340-
begin
341-
truncating? ? truncate(args) : args.inspect
342-
rescue Exception
343-
'<Unable to inspect arguments>'
344-
end
345-
end
346-
347-
def prefix(event)
348-
"#{event.address.to_s} | #{event.database_name}.#{event.command_name}"
349-
end
350-
351-
def truncate(command)
352-
((s = command.inspect).length > LOG_STRING_LIMIT) ? "#{s[0..LOG_STRING_LIMIT]}..." : s
353-
end
354-
355-
def truncating?
356-
@truncating ||= (options[:truncate_logs] != false)
357-
end
358-
end
359-
end
360-
end
361-
362-
To register a custom subscriber, you can do so globally for
363-
all clients or on a per-client basis:
364-
365-
.. code-block:: ruby
366-
367-
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, my_subscriber)
368-
369-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test' )
370-
client.subscribe( Mongo::Monitoring::COMMAND, my_subscriber )
371-
372-
To turn off monitoring, set the client monitoring option to ``false``:
373-
374-
.. code-block:: ruby
375-
376-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :monitoring => false )

source/tutorials/ruby-driver-collection-tasks.txt

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ document in that collection.
1717
You can also explicitly create a collection with various options,
1818
such as setting the maximum size or the documentation validation rules.
1919

20-
Capped Collection
21-
`````````````````
20+
Capped Collections
21+
``````````````````
2222

2323
Capped collections have maximum size or document counts that prevent
2424
them from growing beyond maximum thresholds. All capped collections must
@@ -32,7 +32,9 @@ the ``capped: true`` option along with a ``size`` in bytes.
3232
.. code-block:: ruby
3333

3434
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
35-
client[:artists, capped: true, size: 10000].create
35+
collection = client[:artists, capped: true, size: 10000]
36+
collection.create
37+
collection.capped? # => true
3638

3739
Convert an Existing Collection to Capped
3840
````````````````````````````````````````
@@ -42,9 +44,9 @@ the ``convertToCapped`` command.
4244

4345
.. code-block:: ruby
4446

45-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
47+
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
4648
db = client.database
47-
db.command({ 'convertToCapped' => 'contacts', 'size' => 8192 })
49+
db.command({ 'convertToCapped' => 'artists', 'size' => 10000 })
4850

4951

5052
Document Validation
@@ -102,52 +104,27 @@ documents must contain an ``age`` field which is a number.
102104
}
103105
})
104106

107+
Listing Collections
108+
```````````````````
105109

106-
Change Streams
107-
```````````````
108-
109-
As of version 3.6 of the MongoDB server, a new ```$changeStream`` pipeline stage is supported in the aggregation framework.
110-
Specifying this stage first in an aggregation pipeline allows users to request that notifications are sent for all
111-
changes to a particular collection. The helper method available for change streams on a collection object is preferred
112-
to running a raw aggregation with a $changeStream stage, for the purpose of supporting resumability.
113-
114-
The change stream will attempt to resume one time upon a network error or a server error indicating that a failover is
115-
taking place.
116-
117-
To start receiving change notifications, use the method ``#watch`` on a collection:
118-
119-
.. code-block:: ruby
120-
121-
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
122-
stream = client[:test].watch
123-
124-
Each document can be retrieved and processed by your application as in the following code example:
110+
Use ``collections`` or ``collection_names`` methods on a database
111+
objects to list collections:
125112

126113
.. code-block:: ruby
127114

128-
enum = stream.to_enum
129-
begin
130-
while true
131-
change = enum.next
132-
# do something
133-
sleep(1)
134-
end
135-
rescue => e
136-
# we know for sure it's unrecoverable
137-
log_error(e)
138-
end
139-
140-
The change documents will expose a resume token at the key '_id'. This token can be used to specify a logical starting
141-
point for a new a ChangeStream:
115+
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
116+
database = client.database
142117

143-
.. code-block:: ruby
118+
database.collections # Returns an array of Collection objects.
119+
database.collection_names # Returns an array of collection names as strings.
144120

145-
stream = client[:test].watch([], resume_token: token)
121+
Dropping Collections
122+
````````````````````
146123

147-
A subset of aggregation pipeline stages are available to a change stream and can be passed into the ``#watch`` method:
124+
To drop a collection, call ``drop`` on the collection object.
148125

149126
.. code-block:: ruby
150127

151-
stream = client[:test].watch([ { '$match' => { 'operationType' => 'update' } }])
152-
153-
128+
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
129+
artists = client[:artists]
130+
artists.drop

source/tutorials/ruby-driver-create-client.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,41 @@ be automatically retried:
658658
- ``collection#find_one_and_replace``
659659
- ``collection#find_one_and_delete``
660660
- ``collection#bulk_write`` (for all single statement ops, i.e. not for ``update_many`` or ``delete_many``)
661+
662+
Logging
663+
-------
664+
665+
You can either use the default global driver logger or set your own. To set your own:
666+
667+
.. code-block:: ruby
668+
669+
Mongo::Logger.logger = other_logger
670+
671+
See the `Ruby Logger documentation <http://ruby-doc.org/stdlib-2.2.0/libdoc/logger/rdoc/Logger.html>`_
672+
for more information on the default logger API and available levels.
673+
674+
Changing the Logger Level
675+
`````````````````````````
676+
677+
To change the logger level:
678+
679+
.. code-block:: ruby
680+
681+
Mongo::Logger.logger.level = Logger::WARN
682+
683+
For more control, a logger can be passed to a client for per-client control over logging.
684+
685+
.. code-block:: ruby
686+
687+
my_logger = Logger.new($stdout)
688+
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :logger => my_logger )
689+
690+
Truncation
691+
``````````
692+
693+
The default logging truncates logs at 250 characters by default. To turn this off pass an
694+
option to the client instance.
695+
696+
.. code-block:: ruby
697+
698+
Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :truncate_logs => false )

0 commit comments

Comments
 (0)