Parse join table relation - parse-platform

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) {
}
});

Related

Parse: Retrive all Objects where user is in a relation

I have a Parse Object Event it contain a key attendees, a Parse Relation object. My question is how to retrieve all event where current user is in the attendees relation ? my current code is :
var query = new Parse.Query(Event).equalTo('attendees',currentUser)
query.find({
success:function(list){
}
})
Your query seems Ok, try to verify your data in DB.
cmd from mongo shell
db.getCollection('_Join:attendees:Event').find({'relatedId':currentuserId})

Query Relational data on Parse

I have seen the other questions relating to querying relational data on parse and they don't quite meet my need.
I have 3 related classes: Teacher (1->many) Course (many<-1) Category
Given the above structure, I want to query for all Instructors who have courses that fall under a certain category. Here is what I have so far:
var Category = Parse.Object.extend("Category");
var category = new Category();
category.id = "shdh43ay";
var Course = Parse.Object.extend("Course"); //There are pointers on Course to both Teacher and Category
var courseQuery = new Parse.Query(Course);
courseQuery.equals('Category', category);
var Teacher = Parse.Object.extend("Teacher");
var teacherQuery = new Parse.Query(Teacher);
teacherQuery.matchesQuery(); //Here is where I am stuck
It doesn't look to me like a matchesQuery would do the trick, any ideas would be welcome

Getting objects from Parse Relation that were added in this save

I have a Parse Class Group and there is a parse relation field in it, called people (users, who are in this group). I am implementing a afterSave on "Group". I want to notify users, who are just added by admin into this group.
How do i do that ?
Parse.Cloud.afterSave(Group, function(request){
Parse.Cloud.useMasterKey();
var group = request.object;
var relation = group.relation("people");
//How to get users that are added on this save.
});
To find the new records being added to a relation, you need to inspect the relationsToAdd property of a Relation (its an array):
var newRecords = request.object.op("people").relationsToAdd;
I know this works in beforeSave but have not used it in afterSave tirggers

Cloud Code - include column with user pointer does not work

I have a table which has a user pointer column with column name "user". In Cloud Code, I want to query that table and include the user objects in the result. From documentation and forums it seems that this should work:
var TableWithUserPointer = Parse.Object.extend("TableWithUserPointer");
var query = new Parse.Query(TableWithUserPointer);
query.include("user");
query.find({
useMasterKey: true,
success: function(results) {
for (var i = 0; i < results.length; i++) {
...
But this does not work. Calling results[i].get("user") will return undefined. For some reason which does not make sense to me, using
query.include(Parse.User);
instead of query.include("user") will return a user pointer in the results but the object only contains the user "id"; no other fields of the user object are populated.
Any help would be greatly appreciated! My alternative is gathering all the user pointers and then doing a Parse.User query to get the fully populated user objects, which seems a bit wasteful.
Parse table

how to create a document in a mongodb collection and update another collection in the same time

here is my problem
i have three schema
var UserSchema = new Schema({
username:String,
email:String,
hashed_password:String,
salt:String,
shop_id:{type:Schema.Types.ObjectId,ref:'Shop'},
})
var ShopSchema = new Schema({
owner_id:{type:Schema.Types.ObjectId,ref:'User'},
owner_real_id:String,
owner_real_name:String,
owner_real_location:String,
shop_name:String,
sell_product_ids:[Schema.Types.ObjectId],
})
var ProductSchema = new Schema({
})
it is necessary to sign up the userschema to use the app, but unnecessary to sign up the shopschema unless the user want to sell some stuff. however when the user do sign up the shopschema i need to update the userschema with the shop's _id,
so here is what i did
create the document in shop collection
find the shops _id
update the user collection
as u can see i query the datebase three times,so i was wondering if this can be done in one query in order to save time like
Shop.create(regist_data,function(){
//update the user collection here
})
Just in case u wondering why i need this, its becase i use 'passport' to log user in, and i want to acess the ProductShcema by shop's _id in the req.user, otherwise every time i want to acess the ProductShcema i nend to find the shop's _id and then get the product that belong to the shop's _id.
any way if u have better solution,please let me know.thanx!!!
sorry i think i should've read the mongoose doc more carefully
here is what i figure out
Shop.create(regist_data,function(err,shop){
console.log('shop = '+shop);
User.findByIdAndUpdate(req.user.id,{ $set: { shop_id: shop._id }},{new:true},function(err,data){
if(err){
console.log(err)
}
console.log('new user = '+data);
})
})
it create the shop document and update the users collection , it works for me.

Resources