阅读(4253) (0)

Moralis 批量查询

2022-05-11 09:23:02 更新

如果您需要一次插入、更新或删除多行,建议使用批量查询。

批量查询快速高效,但它们不会触发任何 ​LiveQuery ​事件或任何触发器。

因为批量查询没有开销,所以它们非常快并且直接在数据库上执行。

何时使用:

如果您需要在不触发实时查询或触发器的情况下对数据库运行快速查询。

何时不使用:

如果您需要实时查询或触发器作为这些查询的结果工作。

批量写入

将一个或多个对象插入到一个类中。

选项:

  • className​:要在其中插入行的类名
  • rows​:要插入的行数组,每行需要有包含列数据的更新对象

// insert these 2 rows into the database 
let foodsToInsert = [{update: {"name" : "Apple", "color" : "green"},
                     {update: {"name" : "Orange", "color" : "orange"}]
                     
Moralis.bulkWrite("Food", foodsToInsert)

批量更新

为每个过滤器更新第一个找到的对象上的一个或多个列。

选项:

  • className​:要在其中插入行的类名
  • filters​:要进行的更新数组,每次更新都需要指定过滤器和更新对象。 前者指定选择基于哪一列,后者指定要在选择中更新哪一列。

注意:这个查询总是期望过滤器每次更新都返回 1 行,如果返回多行,则只会更新第一行。

// update the first Food where name is Apple and set color to red 
// also update the first Food where name is Lemon and set color to yellow
let foodsToUpdate = [{filter: {"name" : "Apple"}, update:{ "color" : "red"}},
                     {filter: {"name" : "Lemon"}, update:{ "color" : "yellow"}}]
                     
Moralis.bulkUpdate("Food", foodsToUpdate)

批量更新(很多)

为每个过滤器更新所有找到的对象上的一列或多列。

选项:

  • className​:要在其中插入行的类名
  • filters​:要进行的更新数组,每次更新都需要指定过滤器和更新对象。 前者指定选择基于哪一列,后者指定要在选择中更新哪一列。

// update the all Food where name is Apple and set color to red 
// also update all Food where name is Lemon and set color to yellow
let foodsToUpdate = [{filter: {"name" : "Apple"}, update:{ "color" : "red"}},
                     {filter: {"name" : "Lemon"}, update:{ "color" : "yellow"}}]
                     
Moralis.bulkUpdateMany("Food", foodsToUpdate)

批量删除

删除每个过滤器的第一个找到的对象。

选项:

  • className​:要在其中插入行的类名
  • filters​:要进行的更新数组,每次更新都需要指定过滤器和更新对象。 前者指定选择基于哪一列,后者指定要在选择中更新哪一列。

注意:这个查询总是希望过滤器每次更新都返回 1 行,如果返回多行,则只会删除第一行。

// delete the first Food where name is Apple
// also delete the first Food where color is purple 
let foodsToDelete = [{filter: {"name" : "Apple"}},
                     {filter: {"color" : "purple"}}]
                     
Moralis.bulkDelete("Food", foodsToDelete)

批量删除(很多)

删除为每个过滤器找到的所有对象。

选项:

  • className​:要在其中插入行的类名
  • filters​:要进行的更新数组,每次更新都需要指定过滤器和更新对象。 前者指定选择基于哪一列,后者指定要在选择中更新哪一列。

// deletes all Food where name is Apple
// also delete all Food where color is purple 
let foodsToDelete = [{filter: {"name" : "Apple"}},
                     {filter: {"color" : "purple"}}]
                     
Moralis.bulkDeleteMany("Food", foodsToDelete)