解析JS多关系查询

时间:2017-07-09 01:47:23

标签: javascript parse-platform

我有3个班级(Survey,SurveyItem,VoteSurvey)

  • SurveyItem包含指向Survey
  • 的指针
  • VoteSurvey包含指向Survey的指针和指向SurveyItem的指针

我需要包括我尚未投票的所有调查。

var userSchema = new mongoose.Schema({
    name: {type: String, required: true},
    email: {type: String},
    imageURL: {type: String, default:null},
    providerId: {type: String, required: true},
    token: {type: String, required: true},
    history: [{
        orders:[{type: mongoose.Types.ObjectId, ref:'Order'}],
        restaurant: {type: mongoose.Types.ObjectId, ref: 'Restaurant'}
    }]
}, { timestamps: true});

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

你的班级结构和领域是什么并不完全清楚,所以他们可能是一个更好的方法。但是,如何查询您投票的所有调查,然后找到所有不匹配的调查?例如:

var voteQuery = new Parse.Query('VoteSurvey');
voteQuery.equalTo('user', user);
voteQuery.find().then(function(results) {
  var alreadyVotedSurveyIds = [];

  for (var i=0; i<results.length; i++) {
    alreadyVotedSurveyIds.push(results[i].get('survey').id);
  }

  var surveyQuery = new Parse.Query('Survey');
  surveyQuery.notContainedIn('objectId', alreadyVotedSurveyIds);
  return surveyQuery.find();
}).then(function(notVotedSurveys) {
  // notVotedSurveys is the array of surveys not voted for
});

注意:请记住,Parse查询的默认限制为100.