how remove item with attrib repeat rethinkdb

Viewed 75

i have a problem. I have a query in rethinkdb but show a problem when i try remove items that have one attrib repeat.

data table

[
    {
        codeQR: '100001597182620700',
        numid: '1000081',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597182749578',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597185279006',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183951080',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183951216',
        numid: '1000083',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597185279182',
        numid: '1000083',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597182864764',
        numid: '1000083',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597185307862',
        numid: '1000084',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183974288',
        numid: '1000084',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183002590',
        numid: '1000084',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    }
]

query with rethinkdb

r.db('myDb').table('userSearchData')
.filter(querys=>
    querys('numid').gt('1000080')
    .and(
        querys('numid').lt(String('1000085'))
    )
)
.limit(5)

and this query show it:

[
    {
        codeQR: '100001597182620700',
        numid: '1000081',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597182749578',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597185279006',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183951080',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183951216',
        numid: '1000083',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    }
]

so, the problem is because if you see, the array has a items with attrib numid and i want that only it shows one item of same numid. I resolved it with reduce() javascript function like that

let datas=resultQuery.reduce((arry, val)=>{
    if(arry.length){
        if(!arry.some(val2=>val2.numid===val.numid)){
            arry.push(val)
        }
    }else
        arry.push(val)
    return arry
}, [])
console.log(datas, 'FT array filter', __filename)

and final result is:

[
    {
        codeQR: '100001597182620700',
        numid: '1000081',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597182749578',
        numid: '1000082',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    },
    {
        codeQR: '100001597183951216',
        numid: '1000083',
        user: 'a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5'
    }
]

But only has 3 item of 5 that i ordered

Img shows than me want with that rethinkdb

enter image description here

2 Answers

The result that you are getting is correct. I dont know exactly what your expectations are:

let datas=resultQuery.reduce((arry, val)=>{
    if(arry.length){
        if(!arry.some(val2=>val2.numid===val.numid)){
            arry.push(val)
        }
    }else
        arry.push(val)
    return arry
}, [])
console.log(datas, 'FT array filter', __filename)
  1. at the first item arry.length is false therefore you just add the item
  2. second item you check if numid is not yet existing -> add item
  3. numid already exist -> skip
  4. numid already exist -> skip
  5. numid new -> add item

You can use group to group your data by matching numid. Then select the first entries in those group. I am not sure what the desired selection should be, but you can implement any criteria in the map function.

r.db('myDb').table('userSearchData')
.filter(querys=>
    querys('numid').gt('1000080')
    .and(
        querys('numid').lt(String('1000085'))
    )
)
.group("numid")
.ungroup()
.map(r.row("reduction")(0))
.limit(5) 

Grouping the data will result in buckets as follows:

[
    {
        "group": "1000081",
        "reduction": [
            {
                "codeQR": "100001597182620700",
                "numid": "1000081",
                "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
            }
        ]
    },
    {
        "group": "1000082",
        "reduction": [
            {
                "codeQR": "100001597183951080",
                "numid": "1000082",
                "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
            },
            {
                "codeQR": "100001597182749578",
                "numid": "1000082",
                "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
            },
            {
                "codeQR": "100001597185279006",
                "numid": "1000082",
                "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
            }
        ]
    } 
//....
]

Then you will have to use the ungroup function to continue adding methods to the query. And with map() you select the first element of each reduction.

then your result is:

[
    {
        "codeQR": "100001597182620700",
        "numid": "1000081",
        "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
    },
    {
        "codeQR": "100001597183951080",
        "numid": "1000082",
        "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
    },
    {
        "codeQR": "100001597185279182",
        "numid": "1000083",
        "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
    },
    {
        "codeQR": "100001597183974288",
        "numid": "1000084",
        "user": "a1d0c8d0-7305-43b1-8b4d-d9a6274d76f5"
    }
]
Related