Mongo.OrderedBulk (mongodb-driver v0.7.2) 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
Specs
t() :: %Mongo.OrderedBulk{coll: String.t(), ops: [BulkOps.bulk_op()]}
Link to this section Functions
Specs
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]}]}
Specs
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]}]}
Specs
Appends an insert operation.
Example:
Mongo.OrderedBulk.insert_one(bulk, %{name: "Waldo"})
%Mongo.OrderedBulk{coll: "bulk", ops: [insert: %{name: "Waldo"}]}
Specs
Creates an empty ordered bulk for a collection.
Example:
Mongo.orderedBulk.new("bulk")
%Mongo.OrderedBulk{coll: "bulk", ops: []}
Specs
Appends a bulk operation to the ordered bulk.
Specs
replace_one(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
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]}
]
}
Specs
update_many(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
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]}
]
}
Specs
update_one(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
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]}
]
}
Specs
write( Enumerable.t(), GenServer.server(), String.t(), non_neg_integer(), Keyword.t() ) :: Enumerable.t()
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