我有以下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