Server -- return document in unpublished collection?

Meteor newbie. I’ve been searching and reading for an answer, and either I’m not using the right search terms or in my newbieness I’m not realizing a solution in what I’ve read, so sorry in advance if I’ve overlooked something.

I’m coming from Ruby/Rails/MySQL and considering Meteor for an application (conceptual at the moment) that in part involves reviewing scanned images (and having the user confirm that the images are clear, appropriate, and ready for the next step). However, an image should only be reviewed one time (since we pay users by the image it would be wasteful to pay more than one person for the same image). So initially the database would have all the image records flagged as “not reviewed”. A reviewer would then click a link that would return an image (or image ID) for review – but the image would be picked by the system (server side?) automatically, not from a list or something by the user. I’m not sure via publish/subscribe how to make sure only one reviewer gets assigned the image. From what I read, I don’t think I want to publish the whole collection of unreviewed images (this could be 100K or more) and then have each client “pick” an unreviewed image. Seems like a race condition will ensue among other issues. So is there a way to click a button and have the server side pick one unreviewed image, flag it somehow to prevent another user from getting the same image, and then return just that image/document_id to the user?

Thanks in advance,

Why don’t you add the reviewer id to the image collection, and then publish that one image?

Pseudo Code on Server

Find 1 image not reviewed by this reviewer
If none found, find 1 image not reviewed without assigned reviewer

  • Add reviewer id to that image
    publish image

Now whenever this reviewer comes back to the site, he will see his one picture, until he finishes the reviewing process.

Thank you. I’ll ponder that publish solution. It may be more of lack of mongo knowledge at this point – how to prevent two users from simultaneously finding that same image that is not assigned a reviewer. I’ll dig into Mongo. It’s not bad being a newbie, but it sure is inconvenient sometimes :smile:

You need a way to atomically find and set with MongoDB. That functionality is available with findAndModify.

Meteor does not provide an isomorphic way to use this method, so you will need to invoke it on a rawCollection, which is only available in server code:

var doc = myCollection.rawCollection().findAndModify({options});

Note that if you want the document returned with the modification applied, you will need the new option.

If you include a reviewer field, you can use this as the flag for “being reviewed by” (use it to store the reviewer’s userid).