The parse documentation explains how to retrieve a parent object when querying for children:
const query = new Parse.Query(Comment);
// Include the post data with each comment
query.include("post");
const comments = await query.find();
But it says nothing about going in the other direction (the natural direction, it seems to me): I want to query for posts, and I want each post to include its array of comments. Is there a way to accomplish this?
You can't retrieve one post and all related comments in a single query unless you use aggregate.
Without using aggregate, you will have to perform two queries:
const post = await (new Parse.Query(Post)).get(postId);
const comments = await (new Parse.Query(Comment)).equalTo('post', post).find();
Related
Lets say i create an entry where i can select two collections like this
now , how to detec when someone changed first collection and then apply some filter to data in second collection ?
not sure if you're still looking for the answer but your question ranked high on my search so here's an answer for future visitors.
Say you add a post entry for your Posts collection. In the ./api/post/models/post.js you can create a hook like this
'use strict';
module.exports = {
lifecycles: {
async afterCreate(result, data) {
const id = result.streakID;
const streak = strapi.services.streaks.findOne({ id });
strapi.services.streaks.update({ id }, { counter: streak.counter++ });
},
},
};
My source
Best regards
EDIT: the hook runs on Post creation and accesses the Streak model by way of strapi.services.streaks. To be clear.
I have a created a relation between multiple table like
vertical has many users
tech has many user
team has and belong many users
so when send request from vertical rest API it doesn't have tech and team in response.
all this data are included in response of users table, but I want this all are also included in individual tables as well.
so I am missing anything in this?
if not then how can I add those in response?
Well, let me explain the relations that you've created:
You have linked:
Vertical => Users
Tech => Users
Team => Users
From the above it's clear that you have linked all relations to users collection. So strapi will add an id in users collection to the other relation collections.
so when you do a find like below, bookshelf will by default try to fetch & populate all the relations based on the id stored in the users collection.
// this will fetch the related data also
await strapi.services.users.findOne({id: 1});
Now coming to your doubt on how to get the linked data by querying the other collections, you will need to override the find method in controller and write a manual query using strapi.query('vertical').model to do a join and get the linked data yourself.
const result = await strapi
.query('vertical')
.model.query(qb => {
qb.join('users', 'users.vertical_id', '=', 'vertical.id');
qb.join('tech', 'tech.id', '=', 'users.tech_id');
qb.join('teams_user_relation', 'teams_user_relation.user_id', '=', 'users.id');
qb.join('teams', 'teams.id', '=', 'teams_user_relation.team_id');
})
.fetch();
const fields = result.toJSON();
Please refer to Custom Queries in strapi for more information.
The documentation gives these examples of query definitions. My understanding is items starting with _$ are parameters you can pass to the query.
The only examples I can find of the Query() function inside the transaction processors do not provide parameters.
How can I pass params to my query?
Ala let result = Query('MyGreatQueryReqParam`, name = "john");
As answered on Rocket Chat ...
The buildQuery and Query() methods are covered with examples at the bottom of this doc: https://hyperledger.github.io/composer/latest/api/runtime-api
But the short answer is:
return query('Q1', { inputValue: 'blue' })
I have the same case that is used in the Parse documentation for many-to-many relations using a join table.
In my case I am fetching a list of users by a simple query, but what I need is to know if current user following the user in the list, meaning I want to add a button to the list of users that allows the current user to follow or unfollow users in the list based on their following status.
Is there any chance that I can get this info with one query?
this will help you. see Relational Queries
var following = Parse.Object.extend("Following"); //Following (ParseObject)
var currentUser = Parse.User.current();
var innerQuery = new Parse.Query(following);
innerQuery.exists("status");
var query = new Parse.Query(currentUser);
query.matchesQuery("follow", innerQuery); //follow is pointer type
query.find({
success: function(comments) {
}
});
I am just getting started with LINQ. I am creating an Entity Framework app that uses the canonical Post and Tag model. A Post contains an ID, Text, and Tags, and a Tag contains an ID, a Name, and Posts.
A previous thread on StackOverflow showed me how to query for a Post that matches all Tag objects (A and B and C) in a search list. But how would I query for a Post that matches any Tag (A or B or C) in the list? Thanks for your help.
Stumbled over the answer right after I posted this question. PredicateBuilder to the rescue!
Here's my code, which uses PredicateBuilder. It is set up as an extension method:
public static IQueryable<Note> WhereContainsAnyTags(this IQueryable<Note> notes, IEnumerable<Tag> searchTags)
{
// Initialize
var predicate = PredicateBuilder.False<Note>();
// Select Notes that contain any search Tags
foreach (var searchTag in searchTags)
{
var tag = searchTag;
predicate = predicate.Or(note => note.Tags.Any(t => t.Id == tag.Id));
}
// Set return value
return notes.AsExpandable().Where(predicate);
}
And here is how I call the code:
searchResults = m_ViewModel.ObjectContext.Notes.WhereContainsAnyTags(m_ViewModel.SearchTags);
Not sure if this would work or not, but worth a try anyway I guess if you are already using WhereIn.
var posts = context.Tags.WhereIn(tag => tag.Name, acceptableValues)
.SelectMany(t => t.Posts);
The WhereIn should give you all the tags that are part of the name, and the SelectMany should give you all the posts containing those tags.
You could aslo do it like this with Entity SQL
var post = ctx.Posts.Where("it.Tags.Id IN (1,2,3)");