简体   繁体   中英

RethinkDB: Subscribing to changes of items in an array

user.posts is an array of objects. I'm trying to only fire the callback when a user changes a post or adds a new one. How should I filter this query to make sure I'm only getting updates when a user's post is changed? How do I know which post in the array of posts has changed? Thanks for any help.

r.table('users')('posts').changes().filter(function(posts){
  posts('new_value').difference(posts('old_value'))
 }).run(conn, (err, cursor) => {
  if (err) return
  cursor.each((post) => {
    // This is a single post that just changed, do something with it...
  })
})

users made up of user rows

user = {id: '', posts:[
  {id: '', content: ''},
  {id: '', content: ''},
], other: ''}

您可以编写r.table('users').changes().filter(function(change) { return change('new_val')('posts').setDifference(change('old_val')('posts')).ne([]); })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM