How can I populate in and out of edges in waterline
i am using waterline-oreintdb adopter
here is my query
select *,out.* from sellings // selling is edge class
Thanks
This sounds more like an OrientDB question than an waterline one. One option would be to use a fetch plan (OrientDB Docs):
select #this.toJSON('fetchPlan:in:1 out:1') from selling
Related
Now that groupBy is deprecated, how can I mimic a SQL command like SELECT COUNT(*) FROM table GROUP BY xxx using the Waterline ORM ?
This page recommends using .sum() and .avg() but these methods are for number-type columns. Here I want to be able to count the rows of grouped columns, whatever type it is.
I think for specific groupBy query, you've got two choices.
The first one is a 2 step action.
1) Select all the unique element "group by field" you've got in the database.
2) Then count the record for each unique group by field element.
The second one is to use .sendNativeQuery() wich allow you to send a native SQL query to the datastore (you can use this only if you use a real SQL server and not the embedded Sails.JS database)
sendNativeQuery() Documentation
How can I make a model join query(condition) and sort on relation models on Sails?
Example: I have 4 tables(collections in mongodb) and 4 related models in mongodb:
User: user_id, name
Follow: user_id, following_id (user id is being followed)
Point: user_id, point
Post: name, content, user_id, created_at
So from the post table, I want to make a query to find the posts of users that I'm following and sort by their point. Like this raw sql:
SELECT post.* FROM post
LEFT JOIN user_point up ON up.user_id = post.user_id
WHERE post.user_id IN (1,2,3,4) // assume I got my following_user_ids result is 1,2,3,4 for this case so no need to join follow table
ORDER BY up.point DESC // high point then first return
I don't know how can do this by Sails model? I have read many instructions by got no helps. Almost people said: Sails Association, but it just helps return the relation instead of do the where or order by to sort original model results(is this case: post).
I have worked with Yii2, a PHP framework so with this case I can do it easily:
Post::model()->leftJoin('user_point up', 'up.user_id = post.user_id')->where(['post.user_id' => [1,2,3,4])->orderBy(['up.point' => SORT_DESC])->all();
I'm stucked in Sails, very thanks if someone help me!!!
Because you're using Mongo, and because you need the full power of normal JOIN's, you will probably be forced to use some other ORM solution (i.e. mongodb package on npm) for queries like that.
Why? See the API documentation for sendNativeQuery(), which states native query features are only available for SQL-like DBMS's.
For example I would like to do the following.
Model.find({select: ['id', 'fields.name']}
or something like this?
Model.find({select: ['id', {fields:['name']}]
Is it even possible to do nested selects in waterline?
This question has been answered here
get Selected fields in .populate() waterline-postgresql .populate('fieldName',{select:[]})
select is not supported in .populate(). You can see this github issue. In populate select is not working currently.
This is feature request and it is open issue. hope in next release
waterline team will introduce this feature.
Is there a way to create both a Vertex and Edge in the same query?
I am aware that we can use out_EdgeName/ in_EdgeName to update the edge of a vertex if it already exists in an UPDATE query, but how to do that so that a new edge is created and assigned to the vertex?
Example use case in an Update Upsert query - a Vertex is being created and we require a new Edge to be created for that vertex. Can we do it in the same query or we would need 2 queries at least for that (i.e 2 UPDATE - UPSERTS)?
Taking cue from orientdb sql update edge? :
Something like - UPDATE Persons SET phone=000000, out_Inside=(
UPDATE Edge UPSERT where in=#some_rid/sub-query, out=$this) where person_id=8
This question is actual if you are using new version of orientdb 2.1.
But as far as i know this features was implemented in 2.2 version.
"As far as I can tell, update works (including in/out):"
UPDATE FRIEND SET in=#11:5 WHERE in.name="Samantha" and out.name="Mat"
Although, using a query inside the set clause for in/out will cause it to return array:
UPDATE Friend SET in=(SELECT FROM User WHERE name="Jason") WHERE in.name="Samantha" and out.name="Mat"
Upsert also works, although when creating a new vertex it doesn't set the in/out properties.
You could set the in/out properties your self, like this:
UPDATE Friend SET comment="Wazzzaup", in=#11:5, out=#11:6 UPSERT WHERE in.name="Jason" AND out.name="Mat"
It will result in a longer query when using sub-query for in= and out=, but at least it works (sub-query has same problem as above).
I got this information from issue:
https://github.com/orientechnologies/orientdb/issues/1114#issuecomment-156585776
i am wondering how can i select specific number of child objects instead of taking them all with include?
lets say i have object 'Group' and i need to select last ten students that joined the group.
When i use '.Include("Students"), EF includes all students. I was trying to use Take(10), but i am pretty new to EF and programming as well, so i couldn't figure it out.
Any suggestions?
UPDATED:
ok, i have Group object already retrieved from db like this:
Group group = db.Groups.FirstOrDefault(x=>x.GroupId == id)
I know that i can add Include("Students") statement, but that would bring ALL students, and their number could be quite big whether i need only freshest 10 students. Can i do something like this: var groupWithStudents = group.Students.OrderByDescending(//...).Take(10);?
The problem with this is that Take<> no longer appears in intellisense. Is this clear enough? Thanks for responses
I believe Take(10) would be correct.
var Students= (from c in Groups
orderby c.DateAdded descending
select c).Take(10);
My experience with Take though is that it generates some awful sql.
EDIT:
see if this blog post helps, it talks of conditional includes.
http://blogs.msdn.com/b/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx
Couldn't make Gratzy's suggestion with conditional include work... and found the solution here: http://msdn.microsoft.com/en-us/library/bb896249.aspx
Query would look like this:
group.Students.Attach(group.Students
.CreateSourceQuery()
.OrderByDescending(x=>x.JoinDate)
.Take(10));
This is exactly what i was looking for!
Thanks for all responses anyway!