1

假设我有一个简单的对象

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

我想从数组中删除答案是

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

import com.mongodb.casbah.MongoCollection

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

1 回答 1

2

您需要声明("name" -> "Yes")为 aMongoDBObject因为查看:

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"}}}
于 2013-03-04T12:07:50.520 回答