[Rogue] Update an object inside and embedded array

25 views
Skip to first unread message

Riccardo Sirigu

unread,
Apr 7, 2017, 5:23:31 AM4/7/17
to Lift
I have the following model

class BindedEntityBson extends BsonRecord[BindedEntityBson]{
 
override def meta: BsonMetaRecord[BindedEntityBson] = BindedEntityBson

 
object _id extends ObjectIdField(this)
 
object suggested extends BooleanField(this){
   
override def defaultValue: Boolean = false
  }
 
object excluded extends BooleanField(this){
   
override def defaultValue: Boolean = false
  }
}


class Facility extends MongoRecord[Facility] with ObjectIdPk[Facility]{
 
override def meta: MongoMetaRecord[Facility] = Facility

 
object codify extends StringField(this, 64)
 
object bindedServices extends BsonRecordListField(this, BindedEntityBson)

}

And i would like to update bindedServices inside Facility using the following query

      Facility.where(_.codify eqs hotel.code)
        .and(_.bindedServices.subfield(_._id) eqs new ObjectId(service.id))
        .findAndModify(_.bindedServices.subfield(_.suggested) setTo service.suggested)
        .upsertOne(returnNew = true) 


But mongo throws the following exception

Message: com.mongodb.MongoCommandException: Command failed with error 16837: 'cannot use the part (bindedServices of bindedServices.suggested) to traverse the element ({bindedServices: [ { suggested: true, excluded: false, _id: ObjectId('5831abee46e0fb450583090b') } ]})' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "cannot use the part (bindedServices of bindedServices.suggested) to traverse the element ({bindedServices: [ { suggested: true, excluded: false, _id: ObjectId('5831abee46e0fb450583090b') } ]})", "code" : 16837 }

What am I doing wrong?


Thank you

Tim Nelson

unread,
Apr 7, 2017, 5:44:24 AM4/7/17
to Lift
Hi Riccardo,

I don't believe you can update individual items of a list that way. At least not with rogue. But, you may want to try asking over on their list.

Typically, if you need to modify embedded documents, you're usually better off putting them in a separate collection, because there are limitations on how you can query them when embedded.

Tim

Riccardo Sirigu

unread,
Apr 7, 2017, 5:59:01 AM4/7/17
to Lift
So there is no positional operator in Rogue 

Thank you Tim

Tim Nelson

unread,
Apr 7, 2017, 6:10:31 AM4/7/17
to Lift
Interesting. I don't believe I've ever used the positional operator. It does look like it does what you want. If rogue doesn't support it, you should be able to write it using lift-json or use the mongo-java-driver directly.

Riccardo Sirigu

unread,
Jul 5, 2017, 3:45:55 AM7/5/17
to Lift
Thank you Tim, 

I find out that rogue also have a $ operator so I can do something like

....
.modify(_.bindedServices.$.subfield(_.suggested) setTo service.suggested)

Tim Nelson

unread,
Jul 5, 2017, 9:21:25 AM7/5/17
to Lift
Great. Thanks for following up.
Reply all
Reply to author
Forward
0 new messages