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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Both of those still leave the empty object inside the "schedules" array
There was no good reason why schedules was an array. I changed it to an object and your original answer worked.
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}})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.