【问题标题】:Updating Mongo doc in scala using cashbah使用 cashbah 在 scala 中更新 Mongo doc
【发布时间】:2013-08-28 12:32:43
【问题描述】:

我在 mongodb 中的示例文档是:

{ "_id" : 3, "name" : "sachin", "profilepic" : "images/pics/3.jpg" }
{ "_id" : 1, "name" : "sumit", "profilepic" : "images/pics/2.jpg" }

我想将 status:0 附加到名称为“sachin”的文档中。 我是斯卡拉的新手。我写代码

val query1=MongoDBObject("name"->"sachin")
val query= MongoDBObject(status->0)
coll.update(query1,query)

但它不起作用..

【问题讨论】:

    标签: mongodb scala playframework


    【解决方案1】:

    试试这个对我来说很好用

    coll.update(query1,$set("status"->0)) 
    

    其中 query1 是您的搜索查询

    【讨论】:

      【解决方案2】:

      简单的谷歌搜索在这里会有所帮助:)。这里有一个查找对象的示例:

      coll.findOne(MongoDBObject("title" -> "Star Wars"))
      

      并使用其 id 更新对象:

      coll.update(pfid, $set("year" -> 1994))
      

      您可以在此处找到完整示例:http://janxspirit.blogspot.com/2011/11/introduction-to-casbah-scala-mongodb.html

      我相信您可以在 github 上找到完整的工作示例,只需克隆、运行和使用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 2015-11-18
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多