I have following situation:
User (userId,Name)
Group (groupId,groupName,createdTime)
User ------ created -------> Group1
User ------ created -------> Group2
User ------ created -------> Group3
I want to get the list of group created by user in desending order. I have this query
//g is TitanGraph
g.query().interval("createdTime",0,time).orderBy("createdTime",
Order.DESC).limit(5).vertices();
This will traverse whole graph. but i want for the specific user using userId
Means i will take userId and show all group created by that userId sorted by createdTime
One I was trying
g.query().has(`userId`,'xyz').interval("createdTime",0,time).orderBy("createdTime",
Order.DESC).limit(5).vertices();
Din't work.it was just returning nothing.
You are using a Graph Query when you want a Vertex Query:
https://github.com/thinkaurelius/titan/wiki/Vertex-Centric-Indices
Query the vertex first and then execute the query from there, like:
Vertex v = g.getVertex(userId)
v.query().has(`userId`,'xyz').interval("createdTime",0,time).orderBy("createdTime",
Order.DESC).limit(5).vertices()
I guess this is basically the answer from #MarcoCI written with Titan/Java instead of Gremlin.
What about this?
g.v(userId).outE.interval("createdTime",0,time).orderBy("createdTime", Order.DESC).limit(5);
Related
I am trying to develop a Workout Tracker and am confused as to how I would go about developing a good structure and why it is considered one - and how it'd perform when retrieving data from Google FireStore using Flutter.
In terms of the structure of the data, every User has a subcollection called 'Groups'. A 'Groups' subcollection has 2 fields ('name' & 'description') and will contain more subcollections relating to the different workout template in that group.
Would it be efficient (when implementing methods to retrieve data, etc) to store each subcollection as the name of their workout and have each document in the subcollection as the index of where the specific exercise is placed within the workout. For a more visual representation, I have included an example. '+' shows a subcollection name and '*' shows a document name
-+ Groups
--* Auto-ID
--- 'name'
--- 'description'
---+ Push
-----* 1
------ 'exercise'
------ 'note'
------ 'reps'
------ 'weights'
-----* 2
------ 'exercise'
------ 'note'
------ 'reps'
------ 'weights'
---+ Pull
-----* 1
------ ...
---+ Legs
-----* 1
------ ...
In terms of implementation, I was thinking of using Flutter to retrieve all subcollection names (Push/Pull/Legs) within each group to display it and then to order the workouts within each template by the name of the document.
I'm not sure how efficient this is but I've also considered have Auto-ID for all subcollections and instead just storing it in a field - but why not just save it as the ID if it's going to be unique. In terms of the document names (1/2/3/4..) for each exercise in a template; I was debating wether to have a field called 'position' or just keeping it like this.
Any tips would be gladly appreciated and an explanation with it so I can further understand it!
Lets say I have a social media app. There is a Group model that has a field called invitedUsers which is simply an array of user ids that are a part of that group.
On my backend I have a route that a user hits to join that Group. In that route I do something like the following:
group.invitedUsers = lodash.concat(group.invitedUsers || [], userId)
group.save()
where group is the group that the user wants to join and userId is the id of the user that wants to join the group. Upon save everything is updated properly and the user is now a part of the group.
But what happens if two users hit the route at exactly the same time? How does MongoDB ensure that the group will always have both users ids added via the above method. Is there not a chance that group.invitedUsers could be referencing a stale value if both these group.save() are being triggered around the same time?
Suppose I have a DB and in this DB I have a users table and a groups table where each user is related to a specific group.
In the groups table we have a “data entry” group, a “reviewer” group, an “approver” group etc.
Now assume a user X. This user is added to the “data entry” group and also added to the “reviewer” group, that means the user X has both access rights: data entry and reviewer.
Y is another user who has the “data entry” access right, so when I want to generate a report to get the count for each user and group, the “data entry” user and the “data entry + reviewer” user must be counted separately.
The point here is that those users which belong to both groups must not be counted in the individual groups; for example the “data entry + reviewer” user must not be counted to the “data entry” group.
I'd aggregate twice - first, for each user get all the groups he or she belongs too, and then aggregate that result and count how many each combination appears:
-- Count how many times each combination occurs
SELECT group_combinataions, COUNT(*)
FROM (-- Get the groups for each user
SELECT STRING_AGG(group_name ORDER BY group_name) AS group_combinations
FROM groups
GROUP BY userid) t
GROUP BY group_combinations
I have not figure it out yet, how to retrieve the #rid value from the record metadata using python 3.5 with pyorient client.command to run such SQL query.
Let's said that I have created a User class using the following query in the client.command(query) of pyorient. For simplicity only the queries calls will be shown here:
CREATE User EXTENDS V
CREATE PROPERTY User.name IF NOT EXISTS STRING (MANDATORY TRUE, NOTNULL TRUE)
CREATE INDEX User.name ON User (name) UNIQUE
Let's create a dictionary to hold the pointers of recent created vertex
rec = {}
Now we add some vertex:
rec['Cleo'] = CREATE VERTEX User CLUSTER User CONTENT {'name': 'Cleopatra'}
rec['Alex'] = CREATE VERTEX User CLUSTER User CONTENT {'name': 'Alex'}
Let's see the value of rec['Alex']:
rec['Alex']
[<pyorient.otypes.OrientRecord at 0x7fc39cd69c50>]
Let's said that, we want to know the #rid for Alex, so we can later on use it to create Edges among other classes.
If I run a quety using orientdb studio I can see the #rid:
but, if I run the same query using client.command I get the a list with the two record pointers. So it is the same as having the result from rec['Alex']
so, if I do rec['Alex'][0].oRecordData to get the record data, I only get back:
{'name': 'Alex'}
(1) How can I store the rid in a variable when I create a new vertex?
(2) How can I retrieve the rid for a record when you know, let's said the property name ?
With the python driver you need to do : ret._rid to access the rid
I have a simple chat table. The chat table has a user_id column and a recipient_id column and a boolean agrees_to_chat column.
What I'd like to do, is display the users for which user 1 wants to chat with and whom all other users also want to chat with user 1.
(Note that there will be cases where 1 agrees to chat with 2, but 2 has not gone online to signal a preference yet. Obviously in those cases I don't want a chat to show up.)
Here's what I've come up with so far.
SELECT c1.user_id, c1.recipient_id, c2.user_id, c2.recipient_id FROM chats c1, chats c2
WHERE c1.recipient_id = c2.user_id
AND c1.user_id = c2.recipient_id
AND c2.user_id=1
AND c2.agrees_to_chat=true
AND c1.agrees_to_chat=true
For some reason setting c2.user_id = 1 results in what I want: records where user_id = 1, along with people who have agreed to chat listed in the recipient_id column.
However if I set it to c1.user_id=1 I get the same results flipped over. Namely, now my results are still people who have agreed to chat, but now the recipient_id = 1 for all results, and the user_id is the different users.
This matters to me because if I want to serve data that shows everyone whose agreed to chat with user 1. But if I decide to reference recipient_id in my code, I need to know that won't change...For example, on my computer I noticed that c2.user_id =1 results in what I want, but in this sql fiddle it seems to be that c1.user_id=1 gets what I need... http://sqlfiddle.com/#!15/799a9/2
So what's going on here? Is there something I'm not understanding about my query? Alternatively is there a better query for what I'm trying to achieve?
You don't need all 4 columns, since you already know 1st and 4th (and 2nd and 3rd) will be equal. Use SELECT c2.user_id, c2.recipient_id FROM ... or SELECT c1.user_id, c1.recipient_id FROM .... In case you actually need several copies of the same column from the self-joined tables, you can give names to them: SELECT c1.user_id AS user_id1, c1.recipient_id AS recipient_id1, c2.user_id AS user_id2, c2.recipient_id AS recipient_id2 FROM ...