2017-10-18 90 views
0

我有這個JSON文件:RethinkDB:​​檢查是否在數組對象存在並更換

{ 
    userId: xx, 
    followedAuthors: [ 
    { authorId: abc, timestamp: 123 }, 
    { authorId: xyz, timestamp: 456 }, 
    ] 
} 

當用戶要遵循一個作家,我想寫出檢查,如果該作者已經跟隨查詢,檢查id,以及它是否將新的後續作者追加到數組中。

現在我每次創建一個新條目。 這是我的查詢:

r.table('users') 
.get(userId) 
.replace(user => { 
    return user.merge({ 
    followedTopics: user('followedTopics') 
     .default([]) 
     .setInsert({ topic: topic, timestamp: now }), 
    }) 
    }) 

回答

1

來實現這一點的最好辦法是使用contains(使用謂詞函數),branchappend

+0

'Branch'是這個問題的關鍵,謝謝! – Tres