Last active
January 27, 2022 15:42
-
-
Save bpceee/c6acb6c86f5674b91eba to your computer and use it in GitHub Desktop.
Revisions
-
levionchain revised this gist
Aug 12, 2014 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,12 @@ var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var ThingSchema = new mongoose.Schema({ count: Number, version: Number, }); var Thing = mongoose.model('Thing', ThingSchema); var doAdd = function(id, retryTimes){ if (retryTimes == undefined) retryTimes = 1; Thing.findById(id, function(err, thing){ @@ -15,4 +24,10 @@ var doAdd = function(id, retryTimes){ } ); }); }; //test var id = "53e890fbb9f46000001d8056"; doAdd(id); doAdd(id); doAdd(id); -
levionchain revised this gist
Aug 12, 2014 . 1 changed file with 3 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,11 @@ var doAdd = function(id, retryTimes){ if (retryTimes == undefined) retryTimes = 1; Thing.findById(id, function(err, thing){ thing.count++; Thing.findOneAndUpdate({"_id": id, "version": thing.version}, {$set: {count: thing.count}, $inc: {"version": 1}}, function(err, data){ if (!data) { console.log("retry times " + retryTimes); @@ -24,13 +15,4 @@ var doAdd = function(id, retryTimes){ } ); }); }; -
levionchain created this gist
Aug 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var ThingSchema = new mongoose.Schema({ count: Number, version: Number, }); var doAdd = function(id, retryTimes){ if (retryTimes == undefined) retryTimes = 1; Thing.findById(id, function(err, thing){ thing.count++; var oldVersion = thing.version++; Thing.findOneAndUpdate({"_id": id, "version": oldVersion}, {$set: {count: thing.count, version: thing.version}}, function(err, data){ if (!data) { console.log("retry times " + retryTimes); if (retryTimes == 1000) {console.log("exceed retry times, failed!"); return -1;} doAdd(id, ++retryTimes); } } ); }); }; //test var id = "53e890fbb9f46000001d8056"; doAdd(id); doAdd(id); doAdd(id); doAdd(id); doAdd(id); doAdd(id);