【问题标题】:How to Convert MongoDBObject to JsonString如何将 MongoDBObject 转换为 JsonString
【发布时间】:2023-03-24 17:59:01
【问题描述】:

我的 mongoDb 集合如下所示:

> db.FakeCollection.find().pretty()
{
    "_id" : ObjectId("52b2d71c5c197846fd3a2737"),
    "categories" : [
            {
                    "categoryname" : "entertainment",
                    "categoryId" : "d3ffca550ae44904aedf77cdcbd31d7a",
                    "displayname" : "Entertainment",
                    "subcategories" : [
                            {
                                    "subcategoryname" : "games",
                                    "subcategoryId" : "ff3d0cbeb0eb4960b11b47d7fc64991b",
                                    "displayname" : "Games"
                            }
                    ]
            }
    ]
   }

我想在 Scala 中使用 Specs2 JsonMatchers 和 MongodbCasbah 为以下集合编写一个测试用例。 如何将 DBObject 转换为字符串?

【问题讨论】:

    标签: mongodb scala lift specs2 casbah


    【解决方案1】:

    我相信你的方法在这里有点错误。您的收藏应如下所示:

    class Category extends BsonRecord[Category] {
      def meta = Category
      object categoryname extends StringField(this, 200)
      object categoryId extends StringField(this, 64)
      object displayname extends StringField(this, 100)
      object subcategories extends BsonRecordListField(this, Category)
    }
    object Category extends Category with BsonMetaRecord[Category] {
    }
    
    class FakeCollection extends MongoRecord[FakeCollection] with ObjectIdPk[FakeCollection] {
      def meta = FakeCollection
      object categories extends BsonRecordListField(this, Category)
    }
    object FakeCollection extends FakeCollection with MongoMetaRecord[FakeCollection] {
      override def collectionName = "fakecollection"
      def getEntryByName: List[Category] = {
        FakeCollection.find
      }
    }
    

    用那个方法你可以做到:

    import net.liftweb.json.JsonAST.JValue;
    import net.liftweb.http.js.JsExp;
    import net.liftweb.http.js.JsExp._;
    import net.liftweb.json.JsonDSL.seq2jvalue
    val json: JsExp = seq2JValue(FakeColleciton.find.map(_.asJValue))
    val stringContent = json.toJsCmd; // now it's here, you can match.
    

    看看HERE,看看如何添加 Foursquare Rogue 让您的生活更轻松。

    【讨论】:

    • 我们没有使用 Lift Record 我们使用的是 MongoDB Casbah
    • 每当我处理稳定且已知的架构时,我也更喜欢 Rogue。他们有非常类型安全的方式来查询数据库。然而,在我的一个项目中,我不得不使用 casbah 并使用任意字段构建文档并进行任意查询。 Rogue + Record 不适合这类事情。不过,这对我来说更像是一个例外情况。
    • @AlekseyIzmailov:我为插件道歉,但这是否意味着 Rogue+Record 也不适合更改模式?
    • @ErikAllik 我认为使用 Record 的全部意义在于使用类型安全的方式来访问模式,并且唯一可以实现的方法是模式中没有很多差异。您仍然可以有可选字段,但在这种情况下,目的可能会很快被打败。如果您对架构进行了很多更改,那么只要执行数据迁移就可以了,但是如果您在单个集合中的文档没有通用架构或者在运行时发生更改,那么 Rogue 就不太适合。
    • 我正在考虑在使用 Rogue 时进行动态/增量模式迁移,但我想那时我将不得不坚持迁移。或者只是相应地调整我的 Rogue 架构。
    【解决方案2】:

    简答:

    val doc: com.mongodb.DBObject = ???
    pretty(render(net.liftweb.mongodb.JObjectParser.serialize(doc)))
    

    解释发生了什么的长答案。为了清楚起见,我包含了完整的类型名称:

    import net.liftweb.mongodb.JObjectParser
    import net.liftweb.json.DefaultFormats
    
    // default JSON formats for `parse` and `serialize` below
    implicit val formats = DefaultFormats
    
    // Convert DBObject to JValue:
    val doc: com.mongodb.DBObject = ??? // get it somehow
    val jsonDoc: net.liftweb.json.JValue = JObjectParser.serialize(doc)
    
    // Convert JValue to DBObject:
    val doc2: net.liftweb.json.JObject = ???
    val dbObj: com.mongodb.DBObject = JObjectParser.parse(doc2)
    
    // Render JSON as String:
    import net.liftweb.json._
    pretty(render(jsonDoc))
    // or use compactRender, compact(render(jsonDoc)), etc
    

    要比较 JSON 文档,可以使用 Diff:val Diff(changed, added, deleted) = json1 diff json2

    更多信息在这里:https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/

    您可以使用 specs2 和 Lift Diff 进行测试,例如:

    json1 diff json2 mustEqual Diff(changedJson, addedJson, JNothing)
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多