How can I get all of the properties related to one node type? - cypher

I have a node Country. I know that this node has some properties, but I don't know which. I mean, I know since I've take a look at model. Here is what I've found in documentation:
Country
name: String
iso_2_code: String
iso_3_code: String
region: String
sub.region: String
I know that if I run
MATCH (c:Country)
RETURN c.iso_2_code
I'll get result for one specific property. Is there a query that would as a result return me something like: name, iso_2_code, iso_3_code, region, sub.region?
If I didn't have access to the model how could I list all of the properties that are attached to some node type?

Answering more from a Cypher or openCypher perspective than for a specific implementation. Using the air-routes dataset. There are three things to consider.
Firstly if you know the properties you want, as you mentioned, you can just ask for them explicitly.
MATCH (a:airport {icao:'KDFW'})
RETURN a.city, a.desc
However, if you want all properties, but do not want to list them all, you can just do:
MATCH (a:airport {icao:'KDFW'})
RETURN properties(a)
If you just want the property keys:
MATCH (a:airport {icao:'KDFW'})
RETURN keys(properties(a))
Lastly, if you want the properties plus the label and ID information you can just do:
MATCH (a:airport {icao:'KDFW'})
RETURN a

Related

Rails; How to select for a single database value with a scope

I have a database called students with columns like so
In my rails model with a scope or function, I would like to select for the single age value based on the id. The id column is unique. So something like
scope :get_age, -> (id) { where(id: id).select(:age) }
However this does not seem to work. I would like for the returned value to just be the int 12. Using something like pluck ends up returning an array which I would like to avoid. How would I go about selecting for just the value of 12?
you know that for one id there is just one row (or no rows) so using where or pluck is not ideal, we want something it returns one row or nothing (find, find_by etc)
def self.get_age(id)
find_by(id: id)&.age
end
some_id = 10
User.get_age(some_id)
Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as where, joins and includes. All scope bodies should return an ActiveRecord::Relation or nil to allow for further methods (such as other scopes) to be called on it.
Reference: https://guides.rubyonrails.org/active_record_querying.html#scopes
That's why is not working as you expected.
I think the best alternative would be to do as Ursus suggested. Create a method, a query object, etc.
You basically want to read an attribute(age) of your model object. For that, you don't need to write any specific method or scope.
Here is what you can do in the place where you have the id and you want to fetch the age for that record:
object = YourClass.find(id)
object.age

Convert LinkedHasSet from one type to another

I have a very simple problem, I need to convert a LinkedHashSet that holds one type of object, into another.
So basically what I want to do is something like this(if map could return anything else than TypeB:
LinkedHashSet<TypeA> firstSet
LinkedHashSet<TypeB> secondSet = firstSet.map {
TypeB(firstSet.value1, firstSet.value2)
}
This is mostly written to signalize what I want to achieve, of course it doesn't work. Could someone help me write this in Kotlin?
map returns a List, but you can use mapTo to insert the resulting elements directly into a collection that you provide as its first argument. This collection is also returned so you can assign it to secondSet:
val secondSet: LinkedHashSet<TypeB> = firstSet.mapTo(LinkedHashSet<TypeB>()) {
TypeB(it.value1, it.value2)
}
This is more efficient than using map because it avoids creating an intermediate List to hold the results.

Spring Data Neo4j Cypher get entity type or class name

In my SDN 4 project I have a following Cypher query(a part of query):
(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)
for example I can get id of entity with a following Cypher function id(entity)
How to get entity name or class name ?
Use the labels function
match (entity)<-[:COMMENTED_ON]-(comg:CommentGroup) return id(entity), labels(entity)
For each row returned, you'll get the Neo4j id and array of labels.
Assuming your NodeEntity class labels match at least one of these labels, you can then iterate and load the appropriate class instance yourself.
Generally speaking you shouldn't need to do this however.
If (entity) is polymorphic, SDN/OGM will hydrate the correct objects for you. It pretty much does under the hood what I described above, but it also handles matching on interfaces, subclasses, etc.

AliasToBean DTO with known type

All the examples I am finding for using the AliasToBean transformer use the sessions CreateSqlQuery method rather than the CreateQuery method. They also only return the basic value types, and not any object's of the existing mapped types.
I was hoping it would be possible that my DTO have a property of one of my mapped Domain objects, like below, but I am not getting traction. I get the following exception:
Could not find a setter for property '0' in class 'namespace.DtoClass'
My select looks like the following on my mapped classes (I have confirmed the mappings pull correctly):
SELECT
fcs.MeasurementPoint,
fcs.Form,
fcs.MeasurementPoint.IsUnscheduled as ""IsVisitUnscheduled"",
fcs.MultipleEntryAllowed
FROM FormCollectionSchedule fcs
My end query will be more complex, but I wanted to confirm if this AliasToBean method can return mapped domain objects as well as basic field values from tables retrieved via sql.
the query execution looks like the following:
var result = session.CreateQuery(hqlQuery.ToString())
.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof (VisitFormCollectionResult)))
.List<VisitFormCollectionResult>();
note: the VisitFormCollectionResult DTO has more properties, but I wanted to know if I could populate the domain object properties matching the names
update found my problem! I have to explicitly alias each of the fields. once I added an alias, even though the member property on the class matched my DTO's property name, the hydration of the object worked correctly.
The answer to my own question was that each of the individual fields in the select needed an explicit alias matching the property, regardless if the field name already matched the property name of the DTO object:
SELECT
fcs.MeasurementPoint as "MeasurementPoint",
fcs.Form as "Form",
fcs.MeasurementPoint.IsUnscheduled as "IsVisitUnscheduled",
fcs.MultipleEntryAllowed as "MultipleEntryAllowed"
FROM FormCollectionSchedule fcs

HQL "Contains" statement howto?

I have an entity that has a string property called Tags. I would like to query this entity based upon if a certain string is located in Tags property.
So for example, I would have a function IList GetEntityByTag(string tag), this would return all Entity's that have the value of tag in their 'Tags' property.
I tried going through the ICriteria approach... Expression.In(PropertyName, Value) but this is the exact opposite. I need something like Expression.In(Value, PropertyName).
Perhaps IQuery would be a better strategy but I have not been able to find any type of HQL statment for Property CONTAINS 'abc'.
Any help or point of direction would be great thanks!
If you want to know if a tag is a substring in your Tags property, you might want to consider these tips:
You might want to convert both the string you are searching in and searching for to lowercase first. Expression.ilike does this for you. Score.
To find out if your search term is anywhere in the field, you can set the MatchMode parameter in the ilike function to MatchMode.ANYWHERE.
If, as you commented earlier,
let's say my property, 'Tags' =
a;b;c;d;e. I want to know if 'a'
exists in tags. Will
Expression.Like("Tags", "a") return
true?
If 'a;b;c;d;e' is a string, Expression.ilike( "Tags", "a", MatchMode.ANYWHERE ) will return true.
Do you mean Expression.Like(PropertyName, Value)?