mongodb-driver v0.6.2 Mongo.UnorderedBulk View Source
An unordered bulk is filled in the memeory 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.
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 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.
Link to this section Types
t()
View Source
t() :: %Mongo.UnorderedBulk{
coll: String.t(),
deletes: [BulkOps.bulk_op()],
inserts: [BulkOps.bulk_op()],
updates: [BulkOps.bulk_op()]
}
t() :: %Mongo.UnorderedBulk{ coll: String.t(), deletes: [BulkOps.bulk_op()], inserts: [BulkOps.bulk_op()], updates: [BulkOps.bulk_op()] }
Link to this section Functions
delete_many(bulk, doc)
View Source
delete_many(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) ::
Mongo.UnorderedBulk.t()
delete_many(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) :: Mongo.UnorderedBulk.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: []
}
delete_one(bulk, doc)
View Source
delete_one(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) ::
Mongo.UnorderedBulk.t()
delete_one(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) :: Mongo.UnorderedBulk.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: []
}
insert_one(bulk, doc)
View Source
insert_one(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) ::
Mongo.UnorderedBulk.t()
insert_one(Mongo.UnorderedBulk.t(), BulkOps.bulk_op()) :: Mongo.UnorderedBulk.t()
Appends an insert operation.
Example:
Mongo.UnorderedBulk.insert_one(bulk, %{name: "Waldo"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [%{name: "Waldo"}],
updates: []
}
new(coll)
View Source
new(String.t()) :: Mongo.UnorderedBulk.t()
new(String.t()) :: Mongo.UnorderedBulk.t()
Creates an empty unordered bulk for a collection.
Example:
Mongo.UnorderedBulk.new("bulk")
%Mongo.UnorderedBulk{coll: "bulk", deletes: [], inserts: [], updates: []}
push(arg, bulk)
View Source
push(BulkOps.bulk_op(), Mongo.UnorderedBulk.t()) :: Mongo.UnorderedBulk.t()
push(BulkOps.bulk_op(), Mongo.UnorderedBulk.t()) :: Mongo.UnorderedBulk.t()
Appends a bulk operation to the unordered bulk. One of the field (inserts, updates or deletes) will be updated.
replace_one(bulk, filter, replacement, opts \\ [])
View Source
replace_one(
Mongo.UnorderedBulk.t(),
BSON.document(),
BSON.document(),
Keyword.t()
) :: Mongo.UnorderedBulk.t()
replace_one( Mongo.UnorderedBulk.t(), BSON.document(), BSON.document(), Keyword.t() ) :: Mongo.UnorderedBulk.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]}]
}
update_many(bulk, filter, update, opts \\ [])
View Source
update_many(
Mongo.UnorderedBulk.t(),
BSON.document(),
BSON.document(),
Keyword.t()
) :: Mongo.UnorderedBulk.t()
update_many( Mongo.UnorderedBulk.t(), BSON.document(), BSON.document(), Keyword.t() ) :: Mongo.UnorderedBulk.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]}
]
}
update_one(bulk, filter, update, opts \\ [])
View Source
update_one(
Mongo.UnorderedBulk.t(),
BSON.document(),
BSON.document(),
Keyword.t()
) :: Mongo.UnorderedBulk.t()
update_one( Mongo.UnorderedBulk.t(), BSON.document(), BSON.document(), Keyword.t() ) :: Mongo.UnorderedBulk.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]}
]
}
write(enum, top, coll, limit \\ 1000, opts \\ [])
View Source
write(
Enumerable.t(),
GenServer.server(),
String.t(),
non_neg_integer(),
Keyword.t()
) :: Enumerable.t()
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