2014-09-02 144 views
0

我想刪除一個嵌套的對象,但不是消失它被替換爲空對象。RethinkDB - 刪除一個嵌套的對象

這裏是我的文檔的結構:

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

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

] 

比方說,我想刪除「週末」。這裏是我的代碼:

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

現在,如果我看着我的表,這些文件有這樣的:

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

我也試圖改變它遵循語法描述here,通過使其:

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

但我得到了一個意外令牌'}'的錯誤。任何想法是什麼?

回答

1

這應該工作:

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

領域的時間表是一個數組,那是預期?是不是有兩個字段WeekendsWeekdays的對象?這會讓事情變得更容易。

+0

這兩項仍然留下了「時間表」陣列 – BarthesSimpson 2014-09-02 04:45:07

+0

有內部的空餘對象時間表是一個數組沒有理由。我將它改爲一個對象,並且你原來的答案奏效了。 – BarthesSimpson 2014-09-02 05:12:15

0

你的最後一個靠近我什麼工作,只需要一個true添加到嵌套的JSON對象:

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