Skip to content

Commit

Permalink
chore: code examples adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
zookzook committed Sep 20, 2024
1 parent ff0c870 commit 86a20a8
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,25 @@ Failing operations return a `{:error, error}` tuple where `error` is a
Using `$and`

```elixir
Mongo.find(:mongo, "users", %{"$and" => [%{email: "[email protected]"}, %{first_name: "first_name"}]})
@topology
|> Mongo.find("users", %{"$and" => [%{email: "[email protected]"}, %{first_name: "first_name"}]})
|> Enum.to_list()
```

Using `$or`

```elixir
Mongo.find(:mongo, "users", %{"$or" => [%{email: "[email protected]"}, %{first_name: "first_name"}]})
@topology
|> Mongo.find("users", %{"$or" => [%{email: "[email protected]"}, %{first_name: "first_name"}]})
|> Enum.to_list()
```

Using `$in`

```elixir
Mongo.find(:mongo, "users", %{email: %{"$in" => ["[email protected]", "[email protected]"]}})
@topology
|> Mongo.find("users", %{email: %{"$in" => ["[email protected]", "[email protected]"]}})
|> Enum.to_list()
```

## How to use the `Mongo.Stream`?
Expand All @@ -148,7 +154,7 @@ can take place automatically:
```elixir
@topology
|> Mongo.aggregate(collection, pipeline, opts)
|> Enum.map(fn elem -> elem end)
|> Enum.to_list()
```

### Inserts
Expand Down Expand Up @@ -218,7 +224,9 @@ In those cases, driver users should represent documents using a list of tuples (
order. Example:

```elixir
Mongo.find(top, "users", %{}, sort: [last_name: 1, first_name: 1, _id: 1])
@topology
|> Mongo.find("users", %{}, sort: [last_name: 1, first_name: 1, _id: 1])
|> Enum.to_list()
```

The query above will sort users by last name, then by first name and finally by ID. If an Elixir map had been used to
Expand Down Expand Up @@ -962,14 +970,14 @@ You can use the `:timeout` as a global option to override the default value:

```elixir
# Starts an pooled connection
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", timeout: 60_000)
{:ok, top} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", timeout: 60_000)
```

Each single connection uses `60_000` (60 seconds) as the timeout value instead of `15_000`. But you can override the default value by
using the `:timeout` option, when running a single command:

```elixr
Mongo.find(conn, "dogs", %{}, timeout: 120_000)
```elixir
Mongo.find(top, "dogs", %{}, timeout: 120_000)
```

Now the driver will use 120 seconds as the timeout for the single query.
Expand Down Expand Up @@ -1034,7 +1042,7 @@ To create indexes you can call the function `Mongo.create_indexes/4`:

```elixir
indexes = [[key: [files_id: 1, n: 1], name: "files_n_index", unique: true]]
Mongo.create_indexes(topology_pid, "my_collection", indexes, opts)
Mongo.create_indexes(top, "my_collection", indexes, opts)
```

You specify the `indexes` parameter as a keyword list with all options described in the documentation of the [createIndex](https://docs.mongodb.com/manual/reference/command/createIndexes/#dbcmd.createIndexes) command.
Expand Down Expand Up @@ -1080,7 +1088,7 @@ bulk = "bulk"
|> OrderedBulk.delete_one(%{kind: "cat"})
|> OrderedBulk.delete_one(%{kind: "cat"})

result = Mongo.BulkWrite.write(:mongo, bulk, w: 1)
result = Mongo.BulkWrite.write(@topology, bulk, w: 1)
```

In the following example we import 1.000.000 integers into the MongoDB using the stream api:
Expand Down Expand Up @@ -1214,8 +1222,8 @@ iex> Mongo.EventHandler.start()
iex> {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/test")
{:ok, #PID<0.226.0>}
iex> Mongo.find_one(conn, "test", %{})
[info] Received command: %Mongo.Events.CommandStartedEvent{command: [find: "test", ...
[info] Received command: %Mongo.Events.CommandSucceededEvent{command_name: :find, ...
[info] Received command: %Mongo.Events.CommandStartedEvent{command: [find: "test", ...
[info] Received command: %Mongo.Events.CommandSucceededEvent{command_name: :find, ...
```

## Testing
Expand Down

0 comments on commit 86a20a8

Please sign in to comment.