我找不到定義MongoRecord
的方法,在Lift-MongoRecord裏面有一個Map[String,String]
字段。在Lift中定義一個MongoRecord,裏面有一個地圖
的提升文檔說:
所有標準記錄字段的支持。還有對Mongo特定類型的支持; ObjectId,UUID,Pattern,List和Map。
如何定義Map和List字段?
我找不到定義MongoRecord
的方法,在Lift-MongoRecord裏面有一個Map[String,String]
字段。在Lift中定義一個MongoRecord,裏面有一個地圖
的提升文檔說:
所有標準記錄字段的支持。還有對Mongo特定類型的支持; ObjectId,UUID,Pattern,List和Map。
如何定義Map和List字段?
我定義了一個BsonRecordMapField:
class BsonRecordMapField[OwnerType <: BsonRecord[OwnerType], SubRecordType <: BsonRecord[SubRecordType]]
(rec: OwnerType, valueMeta: BsonMetaRecord[SubRecordType])(implicit mf: Manifest[SubRecordType])
extends MongoMapField[OwnerType, SubRecordType](rec: OwnerType) {
import scala.collection.JavaConversions._
override def asDBObject: DBObject = {
val javaMap = new HashMap[String, DBObject]()
for ((key, element) <- value) {
javaMap.put(key.asInstanceOf[String], element.asDBObject)
}
val dbl = new BasicDBObject(javaMap)
dbl
}
override def setFromDBObject(dbo: DBObject): Box[Map[String, SubRecordType]] = {
val mapResult: Map[String, SubRecordType] = (for ((key, dboEl) <- dbo.toMap.toSeq) yield (key.asInstanceOf[String], valueMeta.fromDBObject(dboEl.asInstanceOf[DBObject]))).toMap
setBox(Full(mapResult))
}
override def asJValue = {
val fieldList = (for ((key, elem) <- value) yield JField(key, elem.asJValue)).toList
JObject(fieldList)
}
override def setFromJValue(jvalue: JValue) = jvalue match {
case JNothing | JNull if optional_? => setBox(Empty)
case JObject(fieldList) => val retrievedMap = fieldList.map {
field =>
val key = field.name
val valRetrieved = valueMeta.fromJValue(field.value) openOr valueMeta.createRecord
(key, valRetrieved)
}.toMap
setBox(Full(retrievedMap))
case other => setBox(FieldHelpers.expectedA("JObject", other))
}
}
這是盜賊的隱式查詢:
class BsonRecordMapQueryField[M <: BsonRecord[M], B <: BsonRecord[B]](val field: BsonRecordMapField[M, B])(implicit mf: Manifest[B]) {
def at(key: String): BsonRecordField[M, B] = {
val listBox = field.setFromJValue(JObject(List(JField("notExisting", JInt(0)))))
val rec = listBox.open_!.head._2
new BsonRecordField[M, B](field.owner, rec.meta)(mf) {
override def name = field.name + "." + key
}
}
}
object ExtendedRogue extends Rogue {
implicit def bsonRecordMapFieldToBsonRecordMapQueryField[M <: BsonRecord[M], B <: BsonRecord[B]](f: BsonRecordMapField[M, B])(implicit mf: Manifest[B]): BsonRecordMapQueryField[M, B] = new BsonRecordMapQueryField[M, B](f) (mf)
}
您可以在地圖中現在使用的是在操作。
MongoMapField怎麼樣?
我使用Rogue和MongoMapField不支持正確(值與操作員)。我構建的解決方案具有BsonRecordListField的所有優點和強大功能 – Matroska 2012-05-03 16:15:23
這是你想要的HTTP://www.assembla.com/spaces/liftweb/wiki/Mongo_Record_Embedded_Objects – dotoree 2012-03-02 09:35:10
否,但地圖的對象,我把它定義爲在回答以下 – Matroska 2012-03-02 15:51:35