【问题标题】:nested pull with scala mongodb casbah使用 scala mongodb casbah 进行嵌套拉取
【发布时间】:2013-01-15 22:40:06
【问题描述】:

假设我有一个简单的对象

{
 "id":"xyz"
 "answers" : [{
   "name" : "Yes",
    }, {
    "name" : "No",
  }]
}

我想从数组中删除答案是

我正在尝试这样的事情,但运气不佳:

import com.mongodb.casbah.MongoCollection

val searchObject = MongoDBObject("id"->"xyz");
getCollection().update(searchObject,$pull( "answers" -> ( "name" -> "Yes")));

【问题讨论】:

    标签: scala mongodb casbah


    【解决方案1】:

    您需要将("name" -> "Yes") 声明为MongoDBObject,因为请看:

    scala> $pull( "answers" -> ( "name" -> "Yes"))
    res10: com.mongodb.casbah.query.Imports.DBObject = { "$pull" : { "answers" : [ "name" , "Yes"]}}
    

    这不是你想要的,你要拉一个子文档:

    scala> $pull ( "answers" -> MongoDBObject("name" -> "Yes") )
    res11: com.mongodb.casbah.query.Imports.DBObject = { "$pull" : { "answers" : { "name" : "Yes"}}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多