MATCH (u:User {userId: 'kHM6TQQpDmaYUyCYEZchRiJeQao1'})
with u, apoc.convert.fromJsonMap(u.address) AS address
with u, point({latitude:address.coordinates.coordinates[0] , longitude:address.coordinates.coordinates[1]}) as userLocation
MATCH (u)-[:FOLLOWS {status: 'accepted'}]->(following)
WHERE NOT (u)-[:BLOCK]->(following)
AND following.createdAt CONTAINS ''
WITH following,userLocation, count(following) as totalFollowing
WITH following, totalFollowing,userLocation, apoc.convert.fromJsonMap(following.address) AS address
WITH following ,address,totalFollowing, userLocation,point({latitude:address.coordinates.coordinates[0] , longitude:address.coordinates.coordinates[1]}) as businessLocation
WITH address,point.distance(userLocation,businessLocation)/1000 as distanceInKm,following,totalFollowing
ORDER BY following.createdAt ASC
SKIP 0
LIMIT 10
RETURN following as list ,count(*) as totalCount,distanceInKm,address
The aggregate functions, such as count, sum, collect, min, max, and avg operate over the rows that are grouped by the same values for all the grouping variables. These are all the variables outside the aggregating function.
For example, assume we have the following rows:
a,b,c
1,1,3
1,2,2
1,1,4
Then, ‘return a, b, count(*)’ will the following two rows:
1, 1, 2
1, 2, 1
In your case, you are counting the number of rows that have the exact same values for list, distance, and address.
MATCH (u:User {userId: 'kHM6TQQpDmaYUyCYEZchRiJeQao1'})
WITH u, apoc.convert.fromJsonMap(u.address) AS address
WITH u, point({latitude:address.coordinates.coordinates[0], longitude:address.coordinates.coordinates[1]}) AS userLocation
MATCH (u)-[:FOLLOWS {status: 'accepted'}]->(following)
WHERE NOT (u)-[:BLOCK]->(following)
AND following.name CONTAINS 'zomato'
WITH following, userLocation, count(following) AS totalFollowing
WITH following, totalFollowing, userLocation, apoc.convert.fromJsonMap(following.address) AS address
WITH following, address, totalFollowing, userLocation, point({latitude:address.coordinates.coordinates[0], longitude:address.coordinates.coordinates[1]}) AS businessLocation
WITH address, point.distance(userLocation, businessLocation)/1000 AS distanceInKm, following, totalFollowing
ORDER BY following.name DESC
SKIP 0
LIMIT 10
RETURN following, distanceInKm, address ,totalFollowing
const totalCount = result.records.reduce((sum, record) => {
const count = record.get("totalFollower").toInt();
return sum + count;
}, 0); this is worrk
Each follower is returned on its own row with a count of one, so your reduction is calculating the number of rows.
You can’t return a count if you return the individual rows. You could convert it into a single row by collecting the followers. You can then return a count, although it is given by the size of the follower list.
What is your objective?
"data": {
"followerList": {
"totalCount": 3,
"userList": [
{
"userId": "RMoO3DTP2fTbMLpzjHHfA4jizaf2",
"name": "rohan",
"email": "rohan@gmail.com",
"number": "9340079371",
"image": "abc.png",
"address": {
"coordinates": {
"type": "Point",
"coordinates": [
22.75943,
75.886009
]
}
}
},
{
"userId": "kHM6TQQpDmaYUyCYEZchRiJeQao1",
"name": "shubham",
"email": "shubham@gmail.com",
"number": "9340079371",
"image": "abc.png",
"address": {
"coordinates": {
"type": "Point",
"coordinates": [
22.75943,
75.886009
]
}
}
},
{
"userId": "q1OeGPxCGUVQkKAbDxYB7Ma8a4X2",
"name": "flipcart",
"email": "flipcart@gmail.com",
"number": "9340079371",
"image": "abc.png",
"address": {
"coordinates": {
"type": "Point",
"coordinates": [
22.75943,
75.886009
]
}
}
}
]
}
}
}
Ok, it looks like you want to return a list of detailed user info for the given user’s followers. The first thing to do is to get your query to calculate a map of each user’s details. You can then collect them and count them in a ‘with’ statement. You can then create the final map of the results with the followers list and count and return it.
like this
try {
const query = `
MATCH (u:User {userId: '${userId}'})
WITH u, apoc.convert.fromJsonMap(u.address) AS address
WITH u, point({latitude:address.coordinates.coordinates[0], longitude:address.coordinates.coordinates[1]}) AS userLocation
MATCH (u)<-[:FOLLOWS {status: 'accepted'}]-(follower)
WHERE NOT (u)-[:BLOCK]->(follower)
AND follower.${orderBY} CONTAINS '${search}'
WITH follower, userLocation, count(follower) AS totalFollower
WITH follower, totalFollower, userLocation, apoc.convert.fromJsonMap(follower.address) AS address
WITH follower, address, totalFollower, userLocation, point({latitude:address.coordinates.coordinates[0], longitude:address.coordinates.coordinates[1]}) AS businessLocation
WITH address, point.distance(userLocation, businessLocation)/1000 AS distanceInKm, follower, totalFollower
ORDER BY follower.${orderBY} ${orderType}
SKIP ${offset}
LIMIT ${limit}
RETURN follower, distanceInKm, address ,totalFollower
`;
console.log(query);
const result = await session.run(query);
const totalCount = result.records.reduce((sum, record) => {
const count = record.get("totalFollower").toInt();
return sum + count;
}, 0);
const userList = result.records.map(record => {
const followingProperties = record.get("follower").properties;
const address = record.get("address");
const distanceInKm = record.get("distanceInKm");
return {
...followingProperties,
address,
distanceInKm
};
});
return { userList, totalCount };
}```
You can try to formulate the results in the query. Something like this:
MATCH (u:User {userId: '${userId}'})
WITH u, apoc.convert.fromJsonMap(u.address) AS address
WITH u, point({
latitude:address.coordinates.coordinates[0],
longitude:address.coordinates.coordinates[1]
}) AS userLocation
MATCH (u)<-[:FOLLOWS {status: 'accepted'}]-(follower)
WHERE NOT EXISTS ((u)-[:BLOCK]->(follower))
AND follower.${orderBY} CONTAINS '${search}'
WITH follower, userLocation, apoc.convert.fromJsonMap(follower.address) AS address
WITH follower, userLocation, point({
latitude:address.coordinates.coordinates[0],
longitude:address.coordinates.coordinates[1]
}) AS businessLocation
WITH {
userId: follower.userId,
name: follower.name,
email: follower.email,
number: follower.number,
image: follower.image,
address: {
coordinates: {
type: "point",
coordinates: [
businessLocation.latitude,
businessLocation.longitude
]
}
},
distance: point.distance(userLocation,businessLocation)/1000
} as followerDetails
WITH collect(followerDetails) as followers, count(*) as totalCount
RETURN {
followerList: {
totalCount: totalCount,
userList: followers
}
} as result