Bulk Queries
Bulk Queries are optimised to do large numbers of database operations fast.
If you need to insert, update or delete many rows at once it's recommended to use Bulk Queries.
Bulk Queries are fast and efficient but they don't trigger any LiveQuery events or any Triggers.
Because Bulk Queries have no overhead they are very fast and are executed directly on the database. Bulk Queries only work in Cloud Code.
When to use:
If you need to run fast queries on the database without triggering Live Queries or Triggers.
When not to use:
If you need Live-Queries or Triggers to work as a result of these queries.

Bulk Write

Inserts one or several objects into a Class.

Options:

  • className: Class name in which you want to insert rows
  • rows: Array of rows to be inserted, each row needs to have update object containing the column data
1
// insert these 2 rows into the database
2
let foodsToInsert = [
3
{ update: { name: "Apple", color: "green" } },
4
{ update: { name: "Orange", color: "orange" } },
5
];
6
7
Moralis.bulkWrite("Food", foodsToInsert);
Copied!

Bulk Update

Updates one or several columns on the first found object for each filter.

Options:

  • className: Class name in which you want to insert rows
  • filters: Array of updates to make, each update needs to specify filter and update objects. The former is specifying which column to base the selection on and the latter is specifying which column to update in the selection.
Note: this query expects always the filter to return 1 row for each update, if multiple rows are returned only the first one will be updated.
1
// update the first Food where name is Apple and set color to red
2
// also update the first Food where name is Lemon and set color to yellow
3
let foodsToUpdate = [
4
{ filter: { name: "Apple" }, update: { color: "red" } },
5
{ filter: { name: "Lemon" }, update: { color: "yellow" } },
6
];
7
8
Moralis.bulkUpdate("Food", foodsToUpdate);
Copied!

Bulk Update Many

Updates one or several columns on all found objects for each filter.

Options:

  • className: Class name in which you want to insert rows
  • filters: Array of updates to make, each update needs to specify filter and update objects. The former is specifying which column to base the selection on and the latter is specifying which column to update in the selection.
1
// update the all Food where name is Apple and set color to red
2
// also update all Food where name is Lemon and set color to yellow
3
let foodsToUpdate = [
4
{ filter: { name: "Apple" }, update: { color: "red" } },
5
{ filter: { name: "Lemon" }, update: { color: "yellow" } },
6
];
7
8
Moralis.bulkUpdateMany("Food", foodsToUpdate);
Copied!

Bulk Delete

Deletes the first found object for each filter.

Options:

  • className: Class name in which you want to insert rows
  • filters: Array of updates to make, each update needs to specify filter and update objects. The former is specifying which column to base the selection on and the latter is specifying which column to update in the selection.
Note: this query expects always the filter to return 1 row for each update, if multiple rows are returned only the first one will be deleted.
1
// delete the first Food where name is Apple
2
// also delete the first Food where color is purple
3
let foodsToDelete = [{filter: {"name" : "Apple"}},
4
{filter: {"color" : "purple"}}]
5
6
Moralis.bulkDelete("Food", foodsToDelete)
Copied!

Bulk Delete Many

Deletes all objects found for each filter.

Options:

  • className: Class name in which you want to insert rows
  • filters: Array of updates to make, each update needs to specify filter and update objects. The former is specifying which column to base the selection on and the latter is specifying which column to update in the selection.
1
// deletes all Food where name is Apple
2
// also delete all Food where color is purple
3
let foodsToDelete = [
4
{ filter: { name: "Apple" } },
5
{ filter: { color: "purple" } },
6
];
7
8
Moralis.bulkDeleteMany("Food", foodsToDelete);
Copied!