简体   繁体   English

Rethinkdb追加到数组(如果存在)

[英]Rethinkdb append to array if exists

In RethinkDB, I have a table authors with the following layout: 在RethinkDB中,我的表authors具有以下布局:

{
  id: 12,
  videos: [1,2,3]
}

Now I get new authors with objects like this: 现在我有了这样的对象的新作者:

{
  id: 12,
  videos: [4,5]
}

If the author now already exists, I want to append the new videos 4 and 5 to the list of videos. 如果作者现在已经存在,我想将新的视频45追加到视频列表中。 If author not exists, just insert the document like it is. 如果作者不存在,则按原样插入文档。 My approach was the following, but it didn't work. 我的方法如下,但是没有用。

r.table('authors').getAll(4, 3, 2, 1, {index: 'id'})
    .replace(function(author, index) {
        return r.branch(
            author.eq(null),
            {
                id: index,
                videos: [1,2,3]
            },
            author
        );
    })

-> Response: ->回应:

{
    "deleted": 0 ,
    "errors": 3 ,
    "first_error":  "Expected 2 arguments but found 1." ,
    "inserted": 0 ,
    "replaced": 0 ,
    "skipped": 0 ,
    "unchanged": 0
}

Thanks! 谢谢!

Your logic is very good. 你的逻辑很好。 It is just some syntax issue. 这只是一些语法问题。

Given an author, if the author isn't existed, insert it, otherwise, append the video array, here is what I think of, using your logic: 给定一个作者,如果不存在该作者,则将其插入,否则,使用您的逻辑附加视频数组,这就是我的想法:

var author = {
    id: 12,
  videos: [9, 10]
};

r.table('authors').insert(author).do(
    function (doc) {
        return r.branch(doc('inserted').ne(0),
          r.expr({inserted: 1}),
          r.table('authors').get(author["id"]).update(function(doc) {
            return {videos: doc('videos').union(author["videos"])}
          })
        )
    }
)

If the insert is sucesfully, it means we have no document with the same id , we don't have to do anything. 如果插入成功,则意味着我们没有具有相同id文档,而无需执行任何操作。 Otherwise, we will update the document and append the videos into it. 否则,我们将更新文档并将视频添加到其中。

To updare an array of multiple author, we can use foreach and expr to turn array into ReQL object. 要更新多个作者的数组,我们可以使用foreachexpr将数组转换为ReQL对象。 However, in this case, we use bracket to get field instead of using [] as in JavaScript object 但是,在这种情况下,我们使用方bracket来获取字段,而不是像JavaScript对象中那样使用[]

var authors = [{
    id: 12,
    videos: [90, 91]
},{
    id: 14,
    videos: [1, 2]
}];

r.expr(authors).forEach(function(author) {

  return r.table('authors').insert(author).do(
    function (doc) {
        return r.branch(doc('inserted').ne(0),
          r.expr({inserted: 1}),
          r.table('authors').get(author("id")).update(function(doc) {
            return {videos: doc('videos').union(author("videos"))}
          })
        )
    }
  )

})

Something like this should do it: 像这样的东西应该这样做:

r([4, 3, 2, 1]).foreach(function(id) {
  return r.table('authors').get(id).replace(function(row) {
    return r.branch(row.eq(null), {id: id, videos: [1, 2, 3]}, row);
  });
});

lambda for replace method has only 1 argument. 用于lambda的replace方法只有1个参数。 But you are trying too pass 2 args: author , index . 但是您也尝试通过两个参数: authorindex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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