@@ -98,9 +98,6 @@ defmodule Mongo do
98
98
connecting to a replica set)
99
99
* `:type` - a hint of the topology type. See `t:initial_type/0` for
100
100
valid values (default: `:unknown`)
101
- * `:pool` - The pool module to use, see `DBConnection` for pool dependent
102
- options, this option must be included with all requests contacting the
103
- pool if not `DBConnection.Connection` (default: `DBConnection.Connection`)
104
101
* `:idle` - The idle strategy, `:passive` to avoid checkin when idle and
105
102
`:active` to checking when idle (default: `:passive`)
106
103
* `:idle_timeout` - The idle timeout to ping the database (default: `1_000`)
@@ -408,69 +405,50 @@ defmodule Mongo do
408
405
Selects documents in a collection and returns a cursor for the selected
409
406
documents.
410
407
411
- ## Options
408
+ For all options see https://docs.mongodb.com/manual/reference/command/find/#dbcmd.find
409
+
410
+ Use the underscore style, for example to set the option `singleBatch` use `single_batch`. Another example:
411
+
412
+ Mongo.find(top, "jobs", %{}, batch_size: 2)
412
413
413
- * `:comment` - Associates a comment to a query
414
- * `:cursor_type` - Set to :tailable or :tailable_await to return a tailable
415
- cursor
416
- * `:max_time` - Specifies a time limit in milliseconds
417
- * `:modifiers` - Meta-operators modifying the output or behavior of a query,
418
- see http://docs.mongodb.org/manual/reference/operator/query-modifier/
419
- * `:cursor_timeout` - Set to false if cursor should not close after 10
420
- minutes (Default: true)
421
- * `:sort` - Sorts the results of a query in ascending or descending order
422
- * `:projection` - Limits the fields to return for all matching document
423
- * `:skip` - The number of documents to skip before returning (Default: 0)
424
414
"""
425
415
@ spec find ( GenServer . server , collection , BSON . document , Keyword . t ) :: cursor
426
416
def find ( topology_pid , coll , filter , opts \\ [ ] ) do
427
417
428
- # "find": <string>,
429
- # "filter": <document>,
430
- # "sort": <document>,
431
- # "projection": <document>,
432
- # "hint": <document or string>,
433
- # "skip": <int>,
434
- # "limit": <int>,
435
- # "batchSize": <int>,
436
- # "singleBatch": <bool>,
437
- # "comment": <string>,
438
- # "maxScan": <int>, // Deprecated in MongoDB 4.0
439
- # "maxTimeMS": <int>,
440
- # "readConcern": <document>,
441
- # "max": <document>,
442
- # "min": <document>,
443
- # "returnKey": <bool>,
444
- # "showRecordId": <bool>,
445
- # "tailable": <bool>,
446
- # "oplogReplay": <bool>,
447
- # "noCursorTimeout": <bool>,
448
- # "awaitData": <bool>,
449
- # "allowPartialResults": <bool>,
450
- # "collation": <document>
451
-
452
418
filter = case normalize_doc ( filter ) do
453
419
[ ] -> nil
454
420
other -> other
455
421
end
456
422
457
423
query = [
458
- { "find" , coll } ,
459
- { "filter" , filter } ,
460
- { "limit" , opts [ :limit ] } ,
461
- { "batchSize" , opts [ :batch_size ] } ,
462
- { "projection" , opts [ :projection ] } ,
463
- { "comment" , opts [ :comment ] } ,
464
- { "maxTimeMS" , opts [ :max_time ] } ,
465
- { "skip" , opts [ :skip ] } ,
466
- { "sort" , opts [ :sort ] }
467
- ] ++ Enum . into ( opts [ :modifiers ] || [ ] , [ ] )
424
+ { "find" , coll } ,
425
+ { "filter" , filter } ,
426
+ { "limit" , opts [ :limit ] } ,
427
+ { "hint" , opts [ :hint ] } ,
428
+ { "singleBatch" , opts [ :single_batch ] } ,
429
+ { "readConcern" , opts [ :read_concern ] } ,
430
+ { "max" , opts [ :max ] } ,
431
+ { "min" , opts [ :min ] } ,
432
+ { "collation" , opts [ :collation ] } ,
433
+ { "returnKey" , opts [ :return_key ] } ,
434
+ { "showRecordId" , opts [ :show_record_id ] } ,
435
+ { "tailable" , opts [ :tailable ] } ,
436
+ { "oplogReplay" , opts [ :oplog_replay ] } ,
437
+ { "tailable" , opts [ :tailable ] } ,
438
+ { "noCursorTimeout" , opts [ :no_cursor_timeout ] } ,
439
+ { "awaitData" , opts [ :await_data ] } ,
440
+ { "batchSize" , opts [ :batch_size ] } ,
441
+ { "projection" , opts [ :projection ] } ,
442
+ { "comment" , opts [ :comment ] } ,
443
+ { "maxTimeMS" , opts [ :max_time ] } ,
444
+ { "skip" , opts [ :skip ] } ,
445
+ { "sort" , opts [ :sort ] }
446
+ ]
468
447
469
448
query = filter_nils ( query )
470
449
471
- opts = if Keyword . get ( opts , :cursor_timeout , true ) , do: opts , else: [ { :no_cursor_timeout , true } | opts ]
472
- drop = ~w( projection comment max_time skip sort modifiers limit cursor_timeout) a
473
- opts = cursor_type ( opts [ :cursor_type ] ) ++ Keyword . drop ( opts , drop )
450
+ drop = ~w( limit hint single_batch read_concern max min collation return_key show_record_id tailable no_cursor_timeout await_data batch_size projection comment max_time skip sort) a
451
+ opts = Keyword . drop ( opts , drop )
474
452
with { :ok , conn , slave_ok , _ } <- select_server ( topology_pid , :read , opts ) ,
475
453
opts = Keyword . put ( opts , :slave_ok , slave_ok ) ,
476
454
do: cursor ( conn , coll , query , opts )
@@ -483,24 +461,16 @@ defmodule Mongo do
483
461
If multiple documents satisfy the query, this method returns the first document
484
462
according to the natural order which reflects the order of documents on the disk.
485
463
486
- ## Options
464
+ For all options see https://docs.mongodb.com/manual/reference/command/find/#dbcmd.find
487
465
488
- * `:comment` - Associates a comment to a query
489
- * `:cursor_type` - Set to :tailable or :tailable_await to return a tailable
490
- cursor
491
- * `:max_time` - Specifies a time limit in milliseconds
492
- * `:modifiers` - Meta-operators modifying the output or behavior of a query,
493
- see http://docs.mongodb.org/manual/reference/operator/query-modifier/
494
- * `:cursor_timeout` - Set to false if cursor should not close after 10
495
- minutes (Default: true)
496
- * `:projection` - Limits the fields to return for all matching document
497
- * `:skip` - The number of documents to skip before returning (Default: 0)
466
+ Use the underscore style, for example to set the option `readConcern` use `read_concern`. Another example:
467
+
468
+ Mongo.find_one(top, "jobs", %{}, read_concern: %{level: "local"})
498
469
"""
499
470
@ spec find_one ( GenServer . server , collection , BSON . document , Keyword . t ) ::
500
471
BSON . document | nil
501
472
def find_one ( conn , coll , filter , opts \\ [ ] ) do
502
473
opts = opts
503
- |> Keyword . delete ( :order_by )
504
474
|> Keyword . delete ( :sort )
505
475
|> Keyword . put ( :limit , 1 )
506
476
|> Keyword . put ( :batch_size , 1 )
@@ -999,10 +969,6 @@ defmodule Mongo do
999
969
1000
970
defp invalid_doc ( doc ) , do: raise ArgumentError , "invalid document containing atom and string keys: #{ inspect doc } "
1001
971
1002
- defp cursor_type ( nil ) , do: [ ]
1003
- defp cursor_type ( :tailable ) , do: [ tailable_cursor: true ]
1004
- defp cursor_type ( :tailable_await ) , do: [ tailable_cursor: true , await_data: true ]
1005
-
1006
972
defp assert_single_doc! ( doc ) when is_map ( doc ) , do: :ok
1007
973
defp assert_single_doc! ( [ ] ) , do: :ok
1008
974
defp assert_single_doc! ( [ { _ , _ } | _ ] ) , do: :ok
0 commit comments