用scala mongodb casbah嵌套拉

时间:2013-01-15 22:40:06

标签: scala mongodb casbah

假设我有一个简单的对象

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

我想从数组中删除答案是

我正在尝试这样的事情而没有太多运气:

import com.mongodb.casbah.MongoCollection

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

1 个答案:

答案 0 :(得分:2)

您需要将("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"}}}