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:

  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

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

Link to this type

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

Link to this section Functions

Link to this function

delete_many(bulk, doc) View Source
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: []
}
Link to this function

delete_one(bulk, doc) View Source
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: []
}
Link to this function

insert_one(bulk, doc) View Source
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: []
}

Creates an empty unordered bulk for a collection.

Example:

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

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

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

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

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

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