View Source Mongo.UnorderedBulk (mongodb-driver v1.5.0)
An unordered bulk is filled in the memory with the bulk operations. These are divided into three lists (inserts, updates, deletes) added. If the unordered bulk is written to the database, the groups are written in the following order:
- inserts
- updates
- deletes
The order within the group is undefined.
Example
alias Mongo.UnorderedBulk
alias Mongo.BulkWrite
bulk = "bulk"
|> UnorderedBulk.new()
|> UnorderedBulk.insert_one(%{name: "Greta"})
|> UnorderedBulk.insert_one(%{name: "Tom"})
|> UnorderedBulk.insert_one(%{name: "Waldo"})
|> UnorderedBulk.update_one(%{name: "Greta"}, %{"$set": %{kind: "dog"}})
|> UnorderedBulk.update_one(%{name: "Tom"}, %{"$set": %{kind: "dog"}})
|> UnorderedBulk.update_one(%{name: "Waldo"}, %{"$set": %{kind: "dog"}})
|> UnorderedBulk.delete_one(%{kind: "dog"})
|> UnorderedBulk.delete_one(%{kind: "dog"})
|> UnorderedBulk.delete_one(%{kind: "dog"})
result = BulkWrite.write(:mongo, bulk, w: 1)
To reduce the memory usage the unordered bulk can be used with streams.
Example
1..1_000_000
|> Stream.map(fn i -> BulkOps.get_insert_one(%{number: i}) end)
|> UnorderedBulk.write(:mongo, "bulk", 1_000)
|> Stream.run()
This example first generates the bulk operation by calling Mongo.BulkOps.get_insert_one\1
. The operation is used as a parameter in the Mongo.UnorderedBulk.write\3
function.
The unordered bulk was created with a buffer of 1000 operations. After 1000 operations, the
unordered bulk is written to the database. Depending on the selected size you can control the speed and memory consumption. The higher the
value, the faster the processing and the greater the memory consumption.
Summary
Functions
Adds the two unordered bulks together.
Appends a delete operation with :limit = 0
.
Appends a delete operation with :limit = 1
.
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
Appends an insert operation.
Creates an empty unordered bulk for a collection.
Appends a bulk operation to the unordered bulk. One of the field (inserts, updates or deletes) will be updated.
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.
Types
@type t() :: %Mongo.UnorderedBulk{ coll: String.t(), deletes: [Mongo.BulkOps.bulk_op()], inserts: [Mongo.BulkOps.bulk_op()], updates: [Mongo.BulkOps.bulk_op()] }
Functions
Adds the two unordered bulks together.
@spec delete_many(t(), BSON.document()) :: t()
Appends a delete operation with :limit = 0
.
Example:
Mongo.UnorderedBulk.delete_many(bulk, %{name: "Waldo"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [{%{name: "Waldo"}, [limit: 0]}],
inserts: [],
updates: []
}
@spec delete_one(t(), BSON.document()) :: t()
Appends a delete operation with :limit = 1
.
Example:
Mongo.UnorderedBulk.delete_one(bulk, %{name: "Waldo"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [{%{name: "Waldo"}, [limit: 1]}],
inserts: [],
updates: []
}
Returns true, if the bulk is empty, that means it contains no inserts, updates or deletes operations
@spec insert_one(t(), BSON.document()) :: t()
Appends an insert operation.
Example:
Mongo.UnorderedBulk.insert_one(bulk, %{name: "Waldo"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [%{name: "Waldo"}],
updates: []
}
Creates an empty unordered bulk for a collection.
Example:
Mongo.UnorderedBulk.new("bulk")
%Mongo.UnorderedBulk{coll: "bulk", deletes: [], inserts: [], updates: []}
@spec push(Mongo.BulkOps.bulk_op(), t()) :: t()
Appends a bulk operation to the unordered bulk. One of the field (inserts, updates or deletes) will be updated.
@spec replace_one(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
Appends a replace operation with :multi = false
.
Example:
Mongo.UnorderedBulk.replace_one(bulk, %{name: "Waldo"}, %{name: "Greta", kind: "dog"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [],
updates: [{%{name: "Waldo"}, %{kind: "dog", name: "Greta"}, [multi: false]}]
}
@spec update_many(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
Appends a update operation with :multi = true
.
Example:
Mongo.UnorderedBulk.update_many(bulk, %{name: "Waldo"}, %{"$set": %{name: "Greta", kind: "dog"}})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [],
updates: [
{%{name: "Waldo"}, %{"$set": %{kind: "dog", name: "Greta"}}, [multi: true]}
]
}
@spec update_one(t(), BSON.document(), BSON.document(), Keyword.t()) :: t()
Appends a update operation with :multi = false
.
Example:
Mongo.UnorderedBulk.update_one(bulk, %{name: "Waldo"}, %{"$set": %{name: "Greta", kind: "dog"}})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [],
updates: [
{%{name: "Waldo"}, %{"$set": %{kind: "dog", name: "Greta"}}, [multi: false]}
]
}
@spec 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