cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Get Page data and total in one query

coolyrat
Node Clone

Is it possible to get total and paging data in one query? Say I have 100 total rows and my output looks like

{
  "total": 100,
  "list": [1, 3, 5, 7, 10]
}

I tried to achieve this with a query like

MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH u.uid as uids, count(u) as total
skip 0 limit 5
RETURN {list: collect(uids), total: total}

OR

MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH count(u) as total, u
WITH u.uid as uids, total
skip 0 limit 5
RETURN {list: collect(uids), total: total}

but the result looks like this

{
  "total": 2,
  "list": [
    1
  ]
}
{
  "total": 3,
  "list": [
    4,
    3,
    6,
    7
  ]
}
1 ACCEPTED SOLUTION

coolyrat
Node Clone

I found this post from Stackoverflow that helps.

For my problem, it can be solved by splitting the Match clause.

MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH count(u) as total
MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH u.uid as uids, total
skip 0 limit 5
RETURN {list: collect(uids), total: total}

But I am not sure about the performance, is it better to execute two queries or is it better like the solution above.

View solution in original post

1 REPLY 1

coolyrat
Node Clone

I found this post from Stackoverflow that helps.

For my problem, it can be solved by splitting the Match clause.

MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH count(u) as total
MATCH (:User {uid: $uid})-[:R1]->(:Chat)<-[:R2]-(:Post)<-[:R3]-(u:User)
WHERE NOT EXISTS(u.someproperty)
WITH u.uid as uids, total
skip 0 limit 5
RETURN {list: collect(uids), total: total}

But I am not sure about the performance, is it better to execute two queries or is it better like the solution above.