Scala,Mongodb与casbah + salat。原子操作

时间:2013-12-31 19:07:30

标签: mongodb scala casbah salat

我有以下DAO方法:

  /**
   * Delete a Program (hard delete), or mark as deleted (soft delete) 
   * will only hard-delete if:
   *   * No other program is based on this one
   *   * The program is not live
   */
  def delete(id: String) = {
    val oid = new ObjectId(id)

    //If we find any programs based on this one, soft delete
    if(ProgramDao.count(MongoDBObject("basedOn" -> oid)) > 0) {
      ProgramDao.collection.update(
        q = MongoDBObject("_id" -> oid), 
        o = MongoDBObject("deletedDate" -> DateTime.now().withTimeAtStartOfDay())
      )
    }
    else {
      ProgramDao.collection.findAndModify( //If we find this program has a liveDate, soft delete
        query = ("_id" $eq oid) ++ ("liveDate" $exists true), 
        update = MongoDBObject("deletedDate" -> DateTime.now().withTimeAtStartOfDay())
      ) match {
        case None => PersonDao.removeById(oid) //Otherwise hard delete
      }
    }
  }

评论解释了粗略的想法。正如你在if块中看到的那样,它必须首先查询程序集合以查看是否有任何程序引用了这个程序集,然后它是否有任何程序并更新记录。在else块中,它试图找到一个带有提供的id的程序,如果它发现它更新了记录,否则它会删除记录,因为它被认为不是实时的。

要检查的是,是否有一种更好的方法可以做到这一点(对于noSQL来说是新手),理想情况下是原子的,但我对任何建议都持开放态度,因为这是我第一次尝试稍微复杂的事情!

干杯! NFV

0 个答案:

没有答案
相关问题