How to select multiple ids with Gremlin? - gremlin

In the console, this returns two vertices:
g.V('615e6de7-3172-458b-bc68-876fd4f5ecb0','059b6de4-a463-4789-a987-7c2833950b5c')
==>v[615e6de7-3172-458b-bc68-876fd4f5ecb0]
==>v[059b6de4-a463-4789-a987-7c2833950b5c]
But when I run it in Gremlin, I get no results:
const idList = `"615e6de7-3172-458b-bc68-876fd4f5ecb0","059b6de4-a463-4789-a987-7c2833950b5c"`;
let traversal = await g.V(idList).toList();
I can get each id individually, but what is the syntax to fetch multiple ids in a single traversal?

The issue in your code is, you are trying to send a string with comma separated ids. Instead you have to send an array of ids.
const ids = ['3994', '3997'];
g.V(ids).properties()

Related

In R with mongolite, how to return _id and extract timestamp from _id

We have the following R code which fetches an entire collection from our mongodb cluster
mongo_uri <- "mongodb+srv://username:password#our-cluster.dwxnd.gcp.mongodb.net/dbname?retryWrites=true&w=majority"
con <- mongolite::mongo('users', db = 'dbname', url = mongo_uri)
users <- con$find('{}')
In the following code, the _id is not returned. However, we want the _id returned, and then we want to use the _id along with the ObjectId.getTimestamp function to extract the date that the document was added to the collection.
Is this possible to do in R with mongolite?
Solved this one on my own.
users <- con$find('{}', fields = '{}')
oid_to_timestamp(users$`_id`[1])

How to remove collection or edge document using for loop in ArangoDB?

I'm using the latest ArangoDB 3.1 on Windows 10.
Here I want to remove the collection document and edge document using the for loop. But I'm getting an error like document not found (vName).
vName contains the many collection names. But I dunno how to use it in for loop.
This is the AQL I am using to remove the documents from the graph:
LET op = (FOR v, e IN 1..1 ANY 'User/588751454' GRAPH 'my_graph'
COLLECT vid = v._id, eid = e._id
RETURN { vid, eid }
)
FOR doc IN op
COLLECT vName = SPLIT(doc.vid,'/')[0],
vid = SPLIT(doc.vid,'/')[1],
eName = SPLIT(doc.eid,'/')[0],
eid = SPLIT(doc.eid,'/')[1]
REMOVE { _key: vid } in vName
Return output im getting from the AQL (Web UI screenshot)
vName is a variable introduced by COLLECT. It is a string with the collection name of a vertex (extracted from vid / v._id). You then try to use it in the removal operation REMOVE { ... } IN vName.
AQL does not support dynamic collection names however, collection names must be known at query compile time:
Each REMOVE operation is restricted to a single collection, and the collection name must not be dynamic.
Source: https://docs.arangodb.com/3.2/AQL/Operations/Remove.html
So, you either have to hardcode the collection into the query, e.g. REMOVE { ... } IN User, or use the special bind parameter syntax for collections, e.g. REMOVE { ... } IN ##coll and bind parameters: {"#coll": "User", ...}.
This also means that REMOVE can only delete documents in a single collection.
It's possible to workaround the limitation somewhat by using subqueries like this:
LET x1 = (FOR doc IN User REMOVE aa IN User)
LET x2 = (FOR doc IN relations REMOVE bb IN relations)
RETURN 1
The variables x1 and x2 are syntactically required and receive an empty array as subquery result. The query also requires a RETURN statement, even if we don't expect any result.
Do not attempt to remove from the same collection twice in the same query though, as it would raise a access after data-modification error.

Creating a graph from Neo4j Data in R

I have a dataset which consists of Users and Repositories, which I query against Neo4j
query = "
MATCH (user:User{name:'mattt'})-->(repo)
MATCH (repo)<--(allUsers:User)
RETURN repo.name, COLLECT(DISTINCT allUsers.name) AS users;
"
q = cypher(neo4j, query)
The relation is between the repo.name and a list of users point to it.
I am having trouble figuring out how to restructure the data to plot this in a graph.
I think you want to use a Cypher query that returns an edgelist, rather than having a list of all users that given user points to. Something like this:
MATCH (u:User)-->(r:Repo)
RETURN u.name AS from, r.name AS to;
Following along from this blog post about network visualization using RNeo4j:
query = "
MATCH (u:User)-->(r:Repo)
RETURN u.name AS from, r.name AS to;
"
edges = cypher(neo4j, query)
Then define a DataFrame for the nodes:
nodes = data.frame(id=unique(c(edges$from, edges$to)))
nodes$label = nodes$id
Then to visualize using the visNetwork libary:
visNetwork(nodes, edges)

nature of SELECT query in MVC and LINQ TO SQL

i am bit confused by the nature and working of query , I tried to access database which contains each name more than once having same EMPid so when i accessed it in my DROP DOWN LIST then same repetition was in there too so i tried to remove repetition by putting DISTINCT in query but that didn't work but later i modified it another way and that worked but WHY THAT WORKED, I DON'T UNDERSTAND ?
QUERY THAT DIDN'T WORK
var names = (from n in DataContext.EmployeeAtds select n).Distinct();
QUERY THAT WORKED of which i don't know how ?
var names = (from n in DataContext.EmployeeAtds select new {n.EmplID, n.EmplName}).Distinct();
why 2nd worked exactly like i wanted (picking each name 1 time)
i'm using mvc 3 and linq to sql and i am newbie.
Both queries are different. I am explaining you both query in SQL that will help you in understanding both queries.
Your first query is:
var names = (from n in DataContext.EmployeeAtds select n).Distinct();
SQL:-
SELECT DISTINCT [t0].[EmplID], [t0].[EmplName], [t0].[Dept]
FROM [EmployeeAtd] AS [t0]
Your second query is:
(from n in EmployeeAtds select new {n.EmplID, n.EmplName}).Distinct()
SQL:-
SELECT DISTINCT [t0].[EmplID], [t0].[EmplName] FROM [EmployeeAtd] AS
[t0]
Now you can see SQL query for both queries. First query is showing that you are implementing Distinct on all columns of table but in second query you are implementing distinct only on required columns so it is giving you desired result.
As per Scott Allen's Explanation
var names = (from n in DataContext.EmployeeAtds select n).Distinct();
The docs for Distinct are clear – the method uses the default equality comparer to test for equality, and the default comparer sees 4 distinct object references. One way to get around this would be to use the overloaded version of Distinct that accepts a custom IEqualityComparer.
var names = (from n in DataContext.EmployeeAtds select new {n.EmplID, n.EmplName}).Distinct();
Turns out the C# compiler overrides Equals and GetHashCode for anonymous types. The implementation of the two overridden methods uses all the public properties on the type to compute an object's hash code and test for equality. If two objects of the same anonymous type have all the same values for their properties – the objects are equal. This is a safe strategy since anonymously typed objects are essentially immutable (all the properties are read-only).
Try this:
var names = DataContext.EmployeeAtds.Select(x => x.EmplName).Distinct().ToList();
Update:
var names = DataContext.EmployeeAtds
.GroupBy(x => x.EmplID)
.Select(g => new { EmplID = g.Key, EmplName = g.FirstOrDefault().EmplName })
.ToList();

cypher: how to return distinct relationship types?

How to return the distinct relationship types from all paths in cypher?
Example query:
MATCH p=(a:Philosopher)-[*]->(b:SchoolType)
RETURN DISTINCT EXTRACT( r in RELATIONSHIPS(p)| type(r) ) as RelationshipTypes
This returns a collection for each path p.
I would like to return a single collection contain the distinct relationship types across all collections.
Here is a link to a graph gist to run the query-
http://gist.neo4j.org/?7851642
You might first collect all relationships on the matched path to a collection "allr", and then get the collection of distinct type(r) from the collection of all relationships,
MATCH p=(a:Philosopher)-[rel*]->(b:SchoolType)
WITH collect(rel) AS allr
RETURN Reduce(allDistR =[], rcol IN allr |
reduce(distR = allDistR, r IN rcol |
distR + CASE WHEN type(r) IN distR THEN [] ELSE type(r) END
)
)
Note, each element 'rcol' in the collection "allr" is in turn a collection of relationships on each matched path.

Resources