Chan Chen Coding...

          Make Auto Incrementing Field in MongoDB

          Side counter method
          One can keep a counter of the current _id in a side document, in a collection dedicated to counters.
          Then use FindAndModify to atomically obtain an id and increment the counter.

          > db.counters.insert({_id: "userId", c: 0});

          > var o = db.counters.findAndModify(
          ...        {query: {_id: "userId"}, update: {$inc: {c: 1}}});
          { "_id" : "userId", "c" : 0 }
          > db.mycollection.insert({_id:o.c, stuff:"abc"});

          > o = db.counters.findAndModify(
          ...        {query: {_id: "userId"}, update: {$inc: {c: 1}}});
          { "_id" : "userId", "c" : 1 }
          > db.mycollection.insert({_id:o.c, stuff:"another one"});
          Once you obtain the next id in the client, you can use it and be sure no other client has it.

          Optimistic loop method
          One can do it with an optimistic concurrency "insert if not present" loop. The following example, in Mongo shell Javascript syntax, demonstrates.

          // insert incrementing _id values into a collection
          function insertObject(o) {
              x = db.myCollection;
              while( 1 ) {
                  // determine next _id value to try
                  var c = x.find({},{_id:1}).sort({_id:-1}).limit(1);
                  var i = c.hasNext() ? c.next()._id + 1 : 1;
                  o._id = i;
                  x.insert(o);
                  var err = db.getLastErrorObj();
                  if( err && err.code ) {
                      if( err.code == 11000 /* dup key */ )
                          continue;
                      else
                          print("unexpected error inserting data: " + tojson(err));
                  }
                  break;
              }
          }
          The above should work well unless there is an extremely high concurrent insert rate on the collection. In that case, there would be a lot of looping potentially.


          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2012-04-14 01:11 Chan Chen 閱讀(356) 評論(0)  編輯  收藏 所屬分類: DB

          主站蜘蛛池模板: 合作市| 马边| 修文县| 庆城县| 安丘市| 敖汉旗| 宁波市| 洪雅县| 自治县| 大姚县| 钦州市| 博爱县| 嘉定区| 临漳县| 建水县| 云浮市| 新田县| 锡林郭勒盟| 邹城市| 巴彦县| 大厂| 嘉禾县| 江源县| 盐津县| 松江区| 察隅县| 于都县| 图木舒克市| 潼关县| 响水县| 博爱县| 邻水| 榆林市| 卢龙县| 池州市| 黑水县| 渝北区| 石首市| 肥城市| 南京市| 永济市|