Mongo.UnorderedBulk (mongodb-driver v0.7.4) 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:

  1. inserts
  2. updates
  3. 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

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.

Link to this section Types

Specs

t() :: %Mongo.UnorderedBulk{
  coll: String.t(),
  deletes: [BulkOps.bulk_op()],
  inserts: [BulkOps.bulk_op()],
  updates: [BulkOps.bulk_op()]
}

Link to this section Functions

Adds the two unordered bulks together.

Specs

delete_many(t(), BulkOps.bulk_op()) :: 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: []
}

Specs

delete_one(t(), BulkOps.bulk_op()) :: 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

Specs

insert_one(t(), BulkOps.bulk_op()) :: t()

Appends an insert operation.

Example:

Mongo.UnorderedBulk.insert_one(bulk, %{name: "Waldo"})
%Mongo.UnorderedBulk{
coll: "bulk",
deletes: [],
inserts: [%{name: "Waldo"}],
updates: []
}

Specs

new(String.t()) :: t()

Creates an empty unordered bulk for a collection.

Example:

Mongo.UnorderedBulk.new("bulk")
%Mongo.UnorderedBulk{coll: "bulk", deletes: [], inserts: [], updates: []}

Specs

push(BulkOps.bulk_op(), t()) :: t()

Appends a bulk operation to the unordered bulk. One of the field (inserts, updates or deletes) will be updated.

Link to this function

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

View Source

Specs

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]}]
}
Link to this function

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

View Source

Specs

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]}
]
}
Link to this function

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

View Source

Specs

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]}
]
}
Link to this function

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

View Source

Specs

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