Search code examples
rethinkdbreql

RethinkDB how to delete duplicated values in a list except for the latest value


I want to reduce a list of adbKeys if there are duplicated title. I want to keep the latest, or the last, value in a list.

Query: r.db('stf').table('users').filter(r.row('adbKeys').count().gt(5))('adbKeys')

Result:

[
    [{
        "adbKeys": [{
            "fingerprint": "1111",
            "title": "[email protected]"
        }, {
            "fingerprint": "1111",
            "title": "@bbbbb"
        }, {
            "fingerprint": "2222",
            "title": "@bbbbb"
        }, {
            "fingerprint": "4444",
            "title": "@aaaaa"
        }, {
            "fingerprint": "5555",
            "title": "@aaaaa"
        }, {
            "fingerprint": "6666",
            "title": "@aaaaa"
        }, {
            "fingerprint": "7777",
            "title": "@aaaaa"
        }, {
            "fingerprint": "8888",
            "title": "@aaaaa"
        }]
    }]
]

So I want to delete following values in the queried user:

[{
        "fingerprint": "1111",
        "title": "@bbbbb"
    },
    {
        "fingerprint": "4444",
        "title": "@aaaaa"
    }, {
        "fingerprint": "5555",
        "title": "@aaaaa"
    }, {
        "fingerprint": "6666",
        "title": "@aaaaa"
    }, {
        "fingerprint": "7777",
        "title": "@aaaaa"
    }
]

In case you need to know about the table.

r.db('stf').table('users').get('xxxxxxxx')

Looks like:

{
    "adbKeys": [{
        "adbKeys": [{
            "fingerprint": "1111",
            "title": "[email protected]"
        }, {
            "fingerprint": "1111",
            "title": "@bbbbb"
        }, {
            "fingerprint": "2222",
            "title": "@bbbbb"
        }, {
            "fingerprint": "4444",
            "title": "@aaaaa"
        }, {
            "fingerprint": "5555",
            "title": "@aaaaa"
        }, {
            "fingerprint": "6666",
            "title": "@aaaaa"
        }, {
            "fingerprint": "7777",
            "title": "@aaaaa"
        }, {
            "fingerprint": "8888",
            "title": "@aaaaa"
        }]
    }],
    "createdAt": {
        "$reql_type$": "TIME",
        "epoch_time": 1601271054.923,
        "timezone": "+00:00"
    },
    "email": "xxxxxxxx",
    "forwards": [],
    "group": "xxxxxxx",
    "groups": {
        "lock": false,
        "quotas": {
            "allocated": {
                "duration": 12960000000,
                "number": 5
            },
            "consumed": {
                "duration": 0,
                "number": 2
            },
            "defaultGroupsDuration": 1296000000,
            "defaultGroupsNumber": 5,
            "defaultGroupsRepetitions": 10,
            "repetitions": 10
        },
        "subscribed": ["213123", "123123123"]
    },
    "ip": "44.33.11.22"
}

Solution

  • I am using the following query, which is not perfect, but working.

    r.db('stf').table('users').filter(r.row('adbKeys').count().ge(20)).update({'adbKeys': r.row("adbKeys").slice(18)}).run(conn)