0

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?

Community
  • 1
  • 1
BarthesSimpson
  • 926
  • 8
  • 21

2 Answers2

1

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.

neumino
  • 4,342
  • 1
  • 18
  • 17
0

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}})
Matt Dodge
  • 10,833
  • 7
  • 38
  • 58