3

我正在使用幻影 1.26.6。

// id is the primary key
case class Motorcycle(id:String, model:String, made:String, capacity:Int)

给一个已存在于 Cassandra 中的 Motorcycle 实例,我想更新模型、制造、容量的值。

// The following does not compile.
 update.where(_.id.eqs(bike.id)).modify(_.model.setTo(bike.model))
 .modify(_.make.setTo(bike.make))
 .modify(_.capacity.setTo(bike.capacity))


 // The following works.   
 val updateQuery = update.where(_.id.eqs(bike.id))

 for {
    _ <- updateQuery.modify(_.model.setTo(bike.model)).future()
    _ <- updateQuery.modify(_.make.setTo(bike.made)).future()
    result <- updateQuery.modify(_.capacity.setTo(bike.capacity)).future()
  } yield (result)

我想知道是否有更好的方法来更新多个字段。

提前感谢您的帮助!

4

1 回答 1

5

您所要做的就是使用and运算符链接多个更新语句。这将在单个查询中执行所有内容。

 val updateQuery = update.where(_.id eqs bike.id)
   .modify(_model setTo bike.model)
   .and(_.make setTo bike.made)
   .and(_.capacity setTo bike.capacity)
   .future()
于 2016-07-15T09:47:27.743 回答