1
1
==============
2
- Administration
2
+ Authentication
3
3
==============
4
4
5
5
.. default-domain:: mongodb
@@ -10,90 +10,6 @@ Administration
10
10
:depth: 1
11
11
:class: singlecol
12
12
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
-
97
13
MongoDB supports a variety of
98
14
:manual:`authentication mechanisms </core/authentication/>`.
99
15
@@ -258,119 +174,3 @@ authentication, see the :manual:`manual
258
174
auth_mech: :gssapi,
259
175
user: 'test',
260
176
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 )
0 commit comments