I have an image gallery with meta stored in mongo. Every time the web site query matches some number of stored images i would like the documents shown
counter field to be incremented. Using findAndModify
would work OK for single document but i can't see a nice way to match multiple documents and update them all.
Is this possible with latest version of mongo? Or any recommend best practices to achieve this ?
thanks fLo
The document format is very simple
{
"name" : "img name",
"description" : "some more info",
"size" : "img size in bytes",
"shown" : "count of times the image was selected by query",
"viewed" : "count of times the image was clicked"
}
And the query is a simple find, then use cursor to loop over results and bump the shown count using document id.. i.e.
db.images.update(
{ _id: "xxxx" },
{ $inc: { shown: 1 } }
)
But i would prefer not to get 100 documents then have to loop over each to update individually. Was hoping to perform find and update in single query.
See Question&Answers more detail:os