ArangoDB insert edge doesn't exists - arangodb

I want to create unique edge between docment collection C1 and C3.
The unique constraint is id and kid.
I use the flow aql to create it, but i get more than one edge in the same id and kid.
how can i achieve it?
sorry for my poor english:)
for i in C1
filter i.id != null and i.id != ''
let exist = first(
for c in C2
filter i.id == c.id and i.kid == c.kid
limit 1
return c
)
filter exist == null
let result = first(
for h in C3
filter i.kid == h.kid
limit 1
return h
)
insert{_from:i._id, _to:result._id, id:i.id, kid:i.kid} INTO C2

My English not very good too^)!
But I think, that i see where you made a mistakes.
First, for your two collection, you can use this code:
LET data = [
{"parent":{"ID":"YOU_MUST_WRITE_HERE_ID_C1"},"child":{"KID":"YOU_MUST_WRITE_HERE_KID_C3"}},
{"parent":{"ID":"YOU_MUST_WRITE_HERE_NEXT_ID_C1"},"child":{"KID":"YOU_MUST_WRITE_HERE_NEXT_KID_C3"}}
]
FOR rel in data
LET parentId = FIRST(
FOR c IN C1
FILTER c.GUID == rel.parent.ID
LIMIT 1
RETURN c._id
)
LET childId = FIRST(
FOR c IN C3
FILTER c.GUID == rel.child.KID
LIMIT 1
RETURN c._id
)
FILTER parentId != null AND childId != null
INSERT { _from: childId, _to: parentId } INTO C2
RETURN NEW
I hope that it help you.
Second - Why do you use the ะก2 collection in this fragment?
let exist = first(
for c in C2

Related

What is the best way to count adjacent edges by their name for each vertex?

I'm trying to count adjacent edges by their collection names.
For example, I have a vertex collection 'User' which has outbound edges to ['visited', 'add_to_cart', 'purchased'].
For each vertex user, I'd like to count adjacent edges by their collection names.
So the final return would be like
{
user_id : "user_1",
visit_count : 3,
add_to_cart_count : 5,
purchase_cnt : 1
}
I've tried the following query, but I doubt it makes the best performance as it uses if else condition and I guess it hinders the overall performance.
The query I tried :
FOR user IN User
FOR v, e, p IN OUTBOUND user visited, add_to_cart, purchased
COLLECT user_id = user.user_id
AGGREGATE
visit_count = SUM(SPLIT(e._id, '/')[0] == 'visited'? 1 : 0),
add_to_cart_count = SUM(SPLIT(e._id, '/')[0] == 'add_to_cart'? 1 : 0),
purchase_cnt = SUM(SPLIT(e._id, '/')[0] == 'purchased'? 1 : 0)
RETURN {
user_id, visit_count, add_to_cart_count, purchase_cnt
}
If IT IS the best way, would there be any index-related gains I can get get use of?
Looking forward to your help :)
Thanks.
thanks to tobias from arangoDB community, I could make it about 30% faster.
LET vis = (FOR e IN visited COLLECT user_id = e._from WITH COUNT INTO n RETURN {user_id, visit_count: n})
LET cart = (FOR e IN add_to_cart COLLECT user_id = e._from WITH COUNT INTO n RETURN {user_id, add_to_cart_count: n})
LET purc = (FOR e IN purchased COLLECT user_id = e._from WITH COUNT INTO n RETURN {user_id, purchase_cnt: n})
FOR x IN UNION(vis, cart, purc)
COLLECT user_id = x.user_id AGGREGATE visit_count = SUM(x.visit_count), add_to_cart_count = SUM(x.add_to_cart_count), purchase_cnt = SUM(x.purchase_cnt)
RETURN {user_id, visit_count, add_to_cart_count, purchase_cnt}
The point he mentioned was to collect the edge collections directly!

How to use WITHIN with 2 geo indexes in one collection?

I have 2 geo indexes in the collection.
I need to find all the documents on the first geo-index and another time on another geo-index.
LET cFrom = (
FOR c IN WITHIN("city", 22.5455400, 114.0683000, 3000, "geofrom")
FOR r IN rcity
FILTER r._from == c._id && r.user == "5010403" && r.type == "freight-m"
LIMIT 1
RETURN r)
LET cTo = (
FOR c IN WITHIN("city", 55.7522200, 37.6155600, 3000, "geoTo")
FOR r IN rcity
FILTER r._to == c._id && r.user == "5010403" && r.type == "freight-r"
LIMIT 100
RETURN r)

Using count, groupBy, as in Slick

How can I convert this sql statement to Slick in most precise, optimal way.
select t.*, count(v.userId) as vote from Talk t inner join Vote v on t.id = v.talkId group by t.id
v.talkId column is a Foreign Key to id column of Talk
Talk Model:
id
description
speaker_id
pledged_date
create_date_time
locked_date
is_approved
Vote Model:
user_id
talk_id
I tried this however it throws exception SlickException: Cannot select Path s2 in Ref s3
val x = for {
t <- models.slick.Talks
v <- models.slick.Votes if t.id === v.talkId
} yield (t, Query(models.slick.Votes).filter(_.talkId === t.id).length)
val y = x.groupBy(_._1.id)
val x = (for {
t <- models.slick.Talks
v <- models.slick.Votes if t.id === v.talkId
} yield (t, v)).groupBy(_._1).map{ case (t,tvs) => (t,tvs.map(_._2).length) }

groovy Hashmap - get the value count from a map

my following code
def traineeDetails = session.traineeDetailsForAuto
on printing gives:
traineeDetails = [name:[Hus, Vin], email:[hus#gmail.com, vin#gmail.com], phone:[9908877654, 9987655432], jobTitle:[SE, ST]]
def count = traineeDetails.name.size() gives correct value =2
but when the map key contains one value
def traineeDetails = session.traineeDetailsForAuto
on printing gives:
traineeDetails = [name:Hus, email:hus#gmail.com, phone:9987766543, jobTitle:SE]
def count= traineeDetails.name.size() gives wrong answer 3 which is the total number of character in name
but here i need to get total count of value that the key name holds..
how to do it?
If you're going to mix types in a map, then you're going to need to check the type:
def count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
Using your examples, it works fine:
traineeDetails = [name:['Hus', 'Vin'], email:['hus#gmail.com', 'vin#gmail.com'], phone:['9908877654', '9987655432'], jobTitle:['SE', 'ST']]
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
assert count == 2
traineeDetails = [name:'Hus', email:'hus#gmail.com', phone:'9987766543', jobTitle:'SE']
count = traineeDetails.name.with { it instanceof Collection ? it.size() : 1 }
assert count == 1
Can you provide actual non-working examples?

Duplicate row check in a datatable using C#

I have data table like below. Here row 2,3,4,5 are not allowed. how do i validate this scenario using C#. Please help. Here the number of columns is not fixed.
A B C
A B C
A B
A C
B C
A B B
B B C
A C B
C# with LinQ
This function eliminates duplicates and null values from columns. If is not null what you are looking for, then you just need to change "DBNull" for what you want.
public static DataTable FilterDataTable(DataTable table)
{
// Erase duplicates
DataView dv = new DataView(table);
table = dv.ToTable(true, table.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray());
// Get Null values
List<DataRow> toErase = new List<DataRow>();
foreach (DataRow item in table.Rows)
for (int i = 0; i < item.ItemArray.Length; i++)
{
if (item.ItemArray[i].GetType().Name == "DBNull")
{ toErase.Add(item); break; }
}
//Erase Null Values
foreach (DataRow item in toErase)
table.Rows.Remove(item);
return table;
}

Resources