Skip to content

Commit 59f526c

Browse files
committed
fixed all options for Mongo.find
1 parent 5d8fc0c commit 59f526c

File tree

2 files changed

+35
-69
lines changed

2 files changed

+35
-69
lines changed

lib/mongo.ex

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ defmodule Mongo do
9898
connecting to a replica set)
9999
* `:type` - a hint of the topology type. See `t:initial_type/0` for
100100
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`)
104101
* `:idle` - The idle strategy, `:passive` to avoid checkin when idle and
105102
`:active` to checking when idle (default: `:passive`)
106103
* `:idle_timeout` - The idle timeout to ping the database (default: `1_000`)
@@ -408,69 +405,50 @@ defmodule Mongo do
408405
Selects documents in a collection and returns a cursor for the selected
409406
documents.
410407
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)
412413
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)
424414
"""
425415
@spec find(GenServer.server, collection, BSON.document, Keyword.t) :: cursor
426416
def find(topology_pid, coll, filter, opts \\ []) do
427417

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-
452418
filter = case normalize_doc(filter) do
453419
[] -> nil
454420
other -> other
455421
end
456422

457423
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+
]
468447

469448
query = filter_nils(query)
470449

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)
474452
with {:ok, conn, slave_ok, _} <- select_server(topology_pid, :read, opts),
475453
opts = Keyword.put(opts, :slave_ok, slave_ok),
476454
do: cursor(conn, coll, query, opts)
@@ -483,24 +461,16 @@ defmodule Mongo do
483461
If multiple documents satisfy the query, this method returns the first document
484462
according to the natural order which reflects the order of documents on the disk.
485463
486-
## Options
464+
For all options see https://docs.mongodb.com/manual/reference/command/find/#dbcmd.find
487465
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"})
498469
"""
499470
@spec find_one(GenServer.server, collection, BSON.document, Keyword.t) ::
500471
BSON.document | nil
501472
def find_one(conn, coll, filter, opts \\ []) do
502473
opts = opts
503-
|> Keyword.delete(:order_by)
504474
|> Keyword.delete(:sort)
505475
|> Keyword.put(:limit, 1)
506476
|> Keyword.put(:batch_size, 1)
@@ -999,10 +969,6 @@ defmodule Mongo do
999969

1000970
defp invalid_doc(doc), do: raise ArgumentError, "invalid document containing atom and string keys: #{inspect doc}"
1001971

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-
1006972
defp assert_single_doc!(doc) when is_map(doc), do: :ok
1007973
defp assert_single_doc!([]), do: :ok
1008974
defp assert_single_doc!([{_, _} | _]), do: :ok

lib/mongo/mongo_db_connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule Mongo.MongoDBConnection do
5555

5656
case result do
5757
{:ok, state} ->
58-
IO.puts inspect state
58+
## IO.puts inspect state
5959
{:ok, state}
6060

6161
{:disconnect, reason, state} ->

0 commit comments

Comments
 (0)