1

lets say i have a simple object

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

I want to remove answer Yes from the array

I'm trying something like this without much luck:

import com.mongodb.casbah.MongoCollection

val searchObject = MongoDBObject("id"->"xyz");
getCollection().update(searchObject,$pull( "answers" -> ( "name" -> "Yes")));
user1255162
  • 1,258
  • 2
  • 9
  • 13

1 Answers1

2

You need to declare ("name" -> "Yes") as a MongoDBObject because look at:

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

Which is not what you want, you want to pull a subdocument:

scala> $pull ( "answers" -> MongoDBObject("name" -> "Yes") )
res11: com.mongodb.casbah.query.Imports.DBObject = { "$pull" : { "answers" : { "name" : "Yes"}}}
Ross
  • 17,861
  • 2
  • 55
  • 73