我正在尝试实现以下目标:基本上只是处理一个 mongodb 文档并向其添加一个时间戳字段,以重建文档被更改的顺序,并在需要时恢复这些条目。
我的方法如下:
@Salat
trait Version[A <: DBEntity] extends ModelCompanion[A, ObjectId] {
def timeStamp: Long = System.currentTimeMillis()
/**
* this method overrides the default salat dao save method in order to make a copy for versioning of Objects
*/
override def save(entity: A) =
{
if (entity.id != null) {
// let salat deserialze the case class into a DBObject
val dbo = dao._grater.asDBObject(entity)
//convert the Object into a Map and append our timestamp
val builder = BasicDBObjectBuilder.start(dbo.toMap()).add("timeStamp", timeStamp)
val copyCollection = MongoDBLayer.mongoDB("history")
//and save it in the historic collection
copyCollection.insert(builder.get())
}
//delegate to the superclass to perform the actual save process
val wr = dao.save(entity)
wr
}
}
有没有更优雅/方便的方式来做到这一点?
或者你的方法是怎样的?
提前致谢,
斯特凡