i have a problem. I have a query in rethinkdb but show a problem when i try remove items that have one attrib repeat.
[
{
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'
}
]
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)
[
{
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
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"
}
]