Reputation: 1489
Currently i have this Publish function
Meteor.publish('Questions', function(limit, skip) {
check([limit, skip], [Number]);
var findAnswers = UserInfo.find({
userId: this.userId
}).fetch();
return Questions.find({}, {
_id: {
$not: findAnswers.questionid
}
}, {
skip: skip,
limit: limit
});
});
So bassicaly there are two Collections, Questions and userInfo (Aka User Answers hmm i think i will change the name) well so on the userInfo i have all the answers of a user.
Now im currently returning 1x1 questions to the user, and this works the limit(1) and the skip work, but now i dont want to send into the user the question that he already answer, i give a try to the $not operator but its still returning the question, to the client (a question that the user already answer).
is $not
the correct operator here? also this will be scalable? lets say in the future the user answer answer 10 question, its there a need of an array? (holding the ids of the answered questions).
Update
Ive made something like this now.
Meteor.publish('Questions', function(limit, skip) {
check([limit, skip], [Number]);
var idsArray =[], //Ids array to hold all the current answered questions ids
findAnswers = UserInfo.find({ //Simple query
userId: this.userId
}).fetch();
_.each(findAnswers, function(id) {
idsArray.push(id.questionid); //forEach to push the ids on the query
});
return Questions.find({}, { //Returning only questions
skip: skip, //with no answers to the client
limit: limit
});
});
Now i got an array of _ids, how can i just send the files that dont match the ids on the query?
Second update
So after some research i find that $nin could do the trick, but this seems to dont work anyways is still returning the 3 documents to the client
Here is more information.
This is my Questions Query.
> Questions.find().fetch()
[ { _id: '2eGSTigujteL4X7qG',
title: 'Where you work?',
body: 'Tell us about your job' },
{ _id: 'ByiJGwgHTm5dk5CBj',
title: 'Where did you study?',
body: 'Tell us about your where did you study' },
{ _id: 'gExyMfiivpb4ZtQTb',
title: 'Did you have childs?',
body: 'Tell us about your childs' } ]
Now this is my idsArray
[ '2eGSTigujteL4X7qG', 'ByiJGwgHTm5dk5CBj' ]
And this is how im doing it on my query.
returnQuestions.find({}, {
_id: {
$nin: [idsArray] //already try without []
},
skip: skip,
limit: limit
});
But this still returning the 3 objects to the client instead of just 1, this should only return the object with the _id:"gExyMfiivpb4ZtQTb"
right? i dont see where is my error.
Upvotes: 0
Views: 22
Reputation: 1766
If your last code snippet is a actual copy+paste from your code you have the wrong find()
syntax (you put your selector in the options object and left the selector object empty). What you intended probably should look like something like this:
returnQuestions.find(
{
_id: {
$nin: idsArray // assuming this already is an Array you of course shouldn't add brackets around it
}, {
skip: skip,
limit: limit
});
And $nin
indeed should be the right operator for your case (also you maybe could use different approaches).
Upvotes: 1