mongodb - Performing bulk update operation: incrementing an object property inside of an array based on existence of name within the array -
i assembling tags collection need perform update operation upsert enabled.
**edit function call add tags collection passed tags array contains tags user entered, name of category tag has been used, , actual title of entry.
the name of category important since want able record how many submissions users use same tag use same category why have entries variable should increment whenever add tag has specific category. array named categories , has objects, every object in array has categoryname property , amount of entries use same category specific tag. have array of title of entries of users, array contains strings.
function updatetags(tags, name, simplestring) { var bulk = mymodel.collection.initializeunorderedbulkop(); for(var t = 0; t < tags.length, t++) { //important bit.. bulk.find({tag : tags[t]}).upsert().update({ //query update document views or new object categories array }) } bulk.execute(cb); }
here sort of algorithmic approach query should perform.
-check existence of tag
--if tag exists check if categoryname properties entries within categories array match category name passed function
---if specific categoryname exists within array, increment entries one.
--if categoryname not exist add object properties categoryname set name passed function , set entries one.
regardless of previous existence of categoryname within categories array, add title (a string) array of title entries.
-if tag not exist add document tag, categories array single object has catagoryname set category name passed , entries set one. add title array of title properties.
well correct in these "bulk" operations because need "more one" update statement here:
function updatetags(tags,name,title,callback) { var bulk = mymodel.collection.initializeorderedbulkop(); tags.foreach(function(tag) { // update category exist bulk.find({ "tag": tag, "categories.name": name }).updateone({ "$inc": { "categories.$.count": 1 }, "$addtoset": { "titles": title } }); // push category not exist bulk.find({ "tag": tag, "categories.name": { "$ne": name } }).updateone({ "$push": { "categories": { "name": name, "count": 1 } }, "$addtoset": { "titles": title } }); // upsert tag not found, nothing else // other add title set bulk.find({ "tag": tag }).upsert().updateone({ "$setoninsert": { "categories": [{ "name": name, "count": 1 }], "titles": [title] }, }); }); bulk.execute(callback); }
so going "each" tag, construct 3 different requests server. conditions set of each of requests "one" match document , modify it.
actually there possibly "two" matches per set, "one" of them can make modifications. because $setoninsert
not apply "matched" document "upsert" operation. operation "nothing" unless "tag" not matched , new document created.
once requests each tag have been built send server , receive response. modified count in response equal number of "tags" sent in argument.
Comments
Post a Comment