RethinkDB - JavaScript 加入多个项目

RethinkDB - JavaScript Join multiple items

我有以下数据结构:

  persons
[
    {
        id:1,
        firstname:'My name',
        talks[
            {
                talk_id:1,
                since:2018-01-01
            },
            {
                talk_id:2,
                since:2018-01-01
            },
            {
                talk_id:3,
                since:2018-01-01
            }
        ]
    },
    {
        id:2,
        firstname:'Other name',
        talks[
            {
                talk_id:1,
                since:2018-01-01
            }           
        ]
    }
]


talks
[
    {
        id:1
        name:'Talk about something'
    },
    {
        id:2
        name:'Talk about something else'
    },
    {
        id:3
        name:'Talk about nothing'
    }
]

我想要一个合并的人员对象,其中包含分配给此人的所有会谈。 我试图通过文档中给出的示例自行解决它,但我不知道如何存档它。

也许我的数据结构不是正确的 noSQL 方式?

这样做的一种方法是:

r.db('tedtalks').table('persons')
.getAll(/* select people here I suppose */)
.merge({
  talks: r.row('talks').map(function (talk) {
    return talk.merge({
      talk: r.db('tedtalks').table('talks').get(talk('talk_id'))
    });
  })
});

您的结果如下:

[
    {
        id:1,
        firstname:'My name',
        talks[
            {
                talk_id:1,
                since:2018-01-01,
                talk: {
                    id: 1,
                    name: 'Talk about something'
                }
            },
            {
                talk_id:2,
                since:2018-01-01,
                talk: {
                    id: 2,
                    name: 'Talk about something else'
                }
            },
            {
                talk_id:3,
                since:2018-01-01,
                talk: {
                    id: 3,
                    name: 'Talk about nothing'
                }
            }
        ]
    },
    {
        id:2,
        firstname:'Other name',
        talks[
            {
                talk_id:1,
                since:2018-01-01,
                talk: {
                    id: 1,
                    name: 'Talk about something'
                }
            }           
        ]
    }
]

.map调用中,你可能想直接return一个对象,去掉talk_id:

    return {
      talk: r.db('tedtalks').table('talks').get(talk('talk_id')),
      since: talk('since')
    };

甚至所有东西都在同一级别(只要键不重叠):

    return r.db('tedtalks').table('talks').get(talk('talk_id')).merge({
      since: talk('since')
    });

好吧,你明白了。