mongodb-driver v0.6.3 Mongo.OrderedBulk View Source

An ordered bulk is filled in the memeory with the bulk operations. If the ordered bulk is written to the database, the order is preserved. Only same types of operation are grouped and only if they have been inserted one after the other.

Example

alias Mongo.OrderedBulk
alias Mongo.BulkWrite

bulk = "bulk"
|> OrderedBulk.new()
|> OrderedBulk.insert_one(%{name: "Greta"})
|> OrderedBulk.insert_one(%{name: "Tom"})
|> OrderedBulk.insert_one(%{name: "Waldo"})
|> OrderedBulk.update_one(%{name: "Greta"}, %{"$set": %{kind: "dog"}})
|> OrderedBulk.update_one(%{name: "Tom"}, %{"$set": %{kind: "dog"}})
|> OrderedBulk.update_one(%{name: "Waldo"}, %{"$set": %{kind: "dog"}})
|> OrderedBulk.update_many(%{kind: "dog"}, %{"$set": %{kind: "cat"}})
|> OrderedBulk.delete_one(%{kind: "cat"})
|> OrderedBulk.delete_one(%{kind: "cat"})
|> OrderedBulk.delete_one(%{kind: "cat"})

BulkWrite.write(:mongo, bulk, w: 1)

This example would not work by using an unordered bulk, because the OrderedBulk.update_many would executed too early.

To reduce the memory usage the ordered bulk can be used with streams as well.

Example

 alias Mongo.OrderedBulk

 1..1000
 |> Stream.map(fn
   1    -> BulkOps.get_insert_one(%{count: 1})
   1000 -> BulkOps.get_delete_one(%{count: 999})
   i    -> BulkOps.get_update_one(%{count: i - 1}, %{"$set": %{count: i}})
 end)
 |> OrderedBulk.write(:mongo, "bulk", 25)
 |> Stream.run()

Of course, this example is a bit silly. A sequence of update operations is created that only work in the correct order.

Link to this section Summary

Functions

Appends a delete operation with :limit = 0.

Appends a delete operation with :limit = 1.

Appends an insert operation.

Creates an empty ordered bulk for a collection.

Appends a bulk operation to the ordered bulk.

Appends a replace operation with :multi = false.

Appends a update operation with :multi = true.

Appends a update operation with :multi = false.

Returns a stream chunk function that can be used with streams. The limit specifies the number of operation hold in the memory while processing the stream inputs.

Link to this section Types

Link to this type

t() View Source
t() :: %Mongo.OrderedBulk{coll: String.t(), ops: [BulkOps.bulk_op()]}

Link to this section Functions

Link to this function

delete_many(bulk, doc) View Source
delete_many(Mongo.OrderedBulk.t(), BulkOps.bulk_op()) :: Mongo.OrderedBulk.t()

Appends a delete operation with :limit = 0.

Example:

Mongo.OrderedBulk.delete_many(bulk, %{name: "Waldo"})
%Mongo.OrderedBulk{coll: "bulk", ops: [delete: {%{name: "Waldo"}, [limit: 0]}]}
Link to this function

delete_one(bulk, doc) View Source
delete_one(Mongo.OrderedBulk.t(), BulkOps.bulk_op()) :: Mongo.OrderedBulk.t()

Appends a delete operation with :limit = 1.

Example:

Mongo.OrderedBulk.delete_one(bulk, %{name: "Waldo"})
%Mongo.OrderedBulk{coll: "bulk", ops: [delete: {%{name: "Waldo"}, [limit: 1]}]}
Link to this function

insert_one(bulk, doc) View Source
insert_one(Mongo.OrderedBulk.t(), BulkOps.bulk_op()) :: Mongo.OrderedBulk.t()

Appends an insert operation.

Example:

Mongo.OrderedBulk.insert_one(bulk, %{name: "Waldo"})
%Mongo.OrderedBulk{coll: "bulk", ops: [insert: %{name: "Waldo"}]}

Creates an empty ordered bulk for a collection.

Example:

Mongo.orderedBulk.new("bulk")
%Mongo.OrderedBulk{coll: "bulk", ops: []}

Appends a bulk operation to the ordered bulk.

Link to this function

replace_one(bulk, filter, replacement, opts \\ []) View Source

Appends a replace operation with :multi = false.

Example:

Mongo.OrderedBulk.replace_one(bulk, %{name: "Waldo"}, %{name: "Greta", kind: "dog"})
%Mongo.OrderedBulk{
coll: "bulk",
ops: [
  update: {%{name: "Waldo"}, %{kind: "dog", name: "Greta"}, [multi: false]}
]
}
Link to this function

update_many(bulk, filter, update, opts \\ []) View Source

Appends a update operation with :multi = true.

Example:

Mongo.OrderedBulk.update_many(bulk, %{name: "Waldo"}, %{"$set": %{name: "Greta", kind: "dog"}})
%Mongo.OrderedBulk{
coll: "bulk",
ops: [
  update: {%{name: "Waldo"}, %{"$set": %{kind: "dog", name: "Greta"}},
   [multi: true]}
]
}
Link to this function

update_one(bulk, filter, update, opts \\ []) View Source

Appends a update operation with :multi = false.

Example:

Mongo.OrderedBulk.update_one(bulk, %{name: "Waldo"}, %{"$set": %{name: "Greta", kind: "dog"}})
%Mongo.OrderedBulk{
coll: "bulk",
ops: [
  update: {%{name: "Waldo"}, %{"$set": %{kind: "dog", name: "Greta"}},
   [multi: false]}
]
}
Link to this function

write(enum, top, coll, limit \\ 1000, opts \\ []) View Source

Returns a stream chunk function that can be used with streams. The limit specifies the number of operation hold in the memory while processing the stream inputs.

The inputs of the stream should be Mongo.BulkOps.bulk_op. See Mongo.BulkOps