BarthesSimpson
BarthesSimpson

Reputation: 954

RethinkDB - delete a nested object

I am trying to delete a nested object, but instead of disappearing it is being replaced by an empty object.

Here's the structure of my documents:

[
    {
      "id":  "0" ,
      "name":  "Employee 1" ,
      "schedules": [
                     {"Weekdays": "yes"},
                     {"Weekends": "no"}
                   ]
     } ,

    {
      "id":  "1" ,
      "name":  "Employee 2" ,
      "schedules": [
                     {"Weekdays": "no"},
                     {"Weekends": "yes"}
                   ]
    }

]

Let's say I want to delete "Weekends". Here's my code:

r.db('shank').table('teachers').replace(r.row.without({'schedules': 'Weekends'})).run(connection, function(err, result) {
    if (err) throw err;
    ;
    //confirmation stuff
        });
});

Now if I look at my table, the documents have this:

"schedules": [
                    {"Weekdays": "yes"},
                    {}
             ]

I also tried changing it to follow the syntax described here, by making it:

r.row.without({'schedules': {'Weekends'}})

but I got an error of an unexpected token '}'. Any idea what's up?

Upvotes: 0

Views: 1214

Answers (2)

Matt Dodge
Matt Dodge

Reputation: 11142

Your last one is close to what worked for me, just need to add a true to the nested JSON object:

r.row.without({'schedules': {'Weekends': true}})

Upvotes: 0

neumino
neumino

Reputation: 4353

This should work:

 r.db('test').table('test').get('0').update(function(doc) {
   return doc.merge({
     schedules: doc("schedules").filter(function(schedule) {
       return schedule.hasFields('Weekends').not()
     })
   })
 })

The field schedules is an array, is that expected? Is there a reason why it's not just an object with two fields Weekends and Weekdays? That would make things way easier.

Upvotes: 1

Related Questions