I am trying to find mutual followers. People who follow me(ID) and also follows one of my follower(followerID) . The relation is [:FOLLOWS{status:accepted}] .
For example,
MATCH (user:User)-[:FOLLOWS {status: 'accepted'}]->(:User {ID:$ID}) RETURN user
This query gives my followers.
How do i find mutual followers when ID and followerID is given?
Try the following query:
MATCH (user:User)-[:FOLLOWS {status: 'accepted'}]->(:User {ID:$ID})
WITH collect(user) as followers
UNWIND followers as follower
MATCH (follower)-[:FOLLOWS {status: 'accepted'}]->(node)
WHERE node in followers
RETURN follower
You can find the triangles like this. You can get the followers of one of your own followers without specifying the followerID.
MATCH (me:User {ID:$ID})<-[:FOLLOWS {status: 'accepted'}]-(mutualFollower:User),
(mutualFollower)-[:FOLLOWS {status: 'accepted'}]->(myFollower:User),
(myFollower)-[:FOLLOWS {status: 'accepted'}]->(me)
// return the list of your followers, with for each the list of followers that you have in common.
RETURN myFollower.ID AS myFollower,
COLLECT(DISTINCT mutualFollower.ID) AS mutualFollowers
Related
i'm trying to get the similarities between one node say me and all the people in the graph not just "friend of friend" which uses the nth order relationship. do i have to go through the whole graph and looping through every user or there's a better way to go?
MATCH (me { name: 'Bradley' })-[:INTEREST]->(stuff)<-
[:INTEREST]-(another_user)
RETURN another_user.name, count(stuff)
ORDER BY count(stuff) DESC
in brief how to replace another_user with all users in the graph in a clean and not costing way
This query should help you:
match (me { name: 'Bradley' })-[:INTEREST]-(stuff) with stuff, collect(stuff) as stuffs match (another_user)-[:INTEREST]-(common_stuff) where common_stuff in stuffs return another_user.name,count(common_stuff) ORDER BY count(common_stuff) DESC
This query will first collect all the items that Bradley is interested in and then iterate through other users and count the items which are common interests between them and Bradley.
This is one user's notes. I want to query and get only the notes of this use with "activeFlag:1". My query object code is
findAccountObj =
{ _id: objectID(req.body.accountId),
ownerId: req.body.userId,
bookId: req.body.bookId,
"notes.activeFlag": 1 };
But this query returns all the notes, including the ones with "activeFlag:0".
How do I fix this?
If you are on v2.2, use elementmatch operator. v3.2 and above allow aggregation and filter to return a subset of a document.
here is an example Retrieve only the queried element in an object array in MongoDB collection
My schema looks like this:
var ArticleSchema = new Schema({
...
category: [{
type: String,
default: ['general']
}],
...
});
I want to parse through all records and find all unique values for this field across all records. This will be sent to the front-end via being called by service for look-ahead search on tagging articles.
We can iterate through every single record and run go through each array value and do a check, but this would be O(n2).
Is there an existing function or another way that has better performance?
You can use the distinct function to get the unique values across all category array fields of all documents:
Article.distinct('category', function(err, categories) {
// categories is an array of the unique category values
});
Put an index on category for best performance.
I'm using Drupal 7 and the Commerce module.
I'm trying to warn the user whenever they add to the cart a product they already own. Getting a list of products already owned by the user (in PHP code) seems quite the challenge, because the connection between Line Item and a Product is in a serialized blob.
I have, as arguments, a list of products currently in the cart, and the user.
If I go the way of EntityFieldQuery, I can easily get all line items pertaining to the given products, but I can't extend the query to filter only completed orders by the given user. (Or can I? That would be great.)
If I go the way of db_select, I can easily get all completed orders for the given user, but I can't bridge the Line Item <-> Product connection.
Any help will be appreciated.
Well, here's the answer in case anyone else is interested:
Apparently there's a table called "field_data_commerce_product", which has an "entity_id" column for the Line Item ID, and a "commerce_product_product_id" column for the Product ID. This table allows db_select to easily connect Orders, Line Items, and Products to achieve the kind of query I asked about.
Here's the complete query:
<?php
// Assuming we have $user populated with the user's ID:
$query = db_select('commerce_order', 'cord');
$query->join('commerce_line_item', 'li', 'cord.order_id = li.order_id');
$query->join('field_data_commerce_product', 'prod', 'li.line_item_id = prod.entity_id');
$query->condition('cord.uid', $user, '=')
->condition('cord.status', 'completed', '=')
->fields('prod', array('commerce_product_product_id'));
$result = $query->execute();
?>
This will return all Product IDs already owned by the given user.
I would like to store some information as follows (note, I'm not wedded to this data structure at all, but this shows you the underlying information I want to store):
{ user_id: 12345, page_id: 2, country: 'DE' }
In these records, user_id is a unique field, but the page_id is not.
I would like to translate this into a Redis data structure, and I would like to be able to run efficient searches as follows:
For user_id 12345, find the related country.
For page_id 2, find all related user_ids and their countries.
Is it actually possible to do this in Redis? If so, what data structures should I use, and how should I avoid the possibility of duplicating records when I insert them?
It sounds like you need two key types: a HASH key to store your user's data, and a LIST for each page that contains a list of related users. Below is an example of how this could work.
Load Data:
> RPUSH page:2:users 12345
> HMSET user:12345 country DE key2 value2
Pull Data:
# All users for page 2
> LRANGE page:2:users 0 -1
# All users for page 2 and their countries
> SORT page:2:users By nosort GET # GET user:*->country GET user:*->key2
Remove User From Page:
> LREM page:2:users 0 12345
Repeat GETs in the SORT to retrieve additional values for the user.
I hope this helps, let me know if there's anything you'd like clarified or if you need further assistance. I also recommend reading the commands list and documentation available at the redis web site, especially concerning the SORT operation.
Since user_id is unique and so does country, keep them in a simple key-value pair. Quering for a user is O(1) in such a case... Then, keep some Redis sets, with key the page_id and members all the user_ids..