首页 文章

如何在sails-mongo中使用自动增量

提问于
浏览
2

我只是在玩sailsjs的概念然后我才知道如果我们使用mongodb作为我们的数据库,我们就不能在风帆中使用自动增量 . 即使对于非主键属性,我也无法使用自动增量 . 对于不是主键的属性,是否有任何特殊方法可以使用自动增量操作?提前致谢

4 回答

  • 8

    如果水线不能自动支持,这就是它的样子,你可以在水线的 beforeCreate lifecycle callback进行 . 它独立于任何数据库适配器 .

    我建议你看看生命周期回调是如何工作的,以便清楚地理解 . 工作流程将类似于以下内容 . 在创建任何记录之前,您将检查 Model 的记录数 . 然后将要创建的记录的 x 字段更新为多于找到的记录并传递batton .

    beforeCreate: function(obj, next){
        Model.count().exec(function(err, cnt){
            if(err) next(err);
            else{
                obj['x'] = cnt + 1;
                next(null);
            }
        })
    }
    

    计算记录并不是完美的方式 . 您可以更改自己喜欢的方式来查找自动递增值的值 . 这个例子的目的是让你直截了当地知道它是如何完成的 . 这不是自动增量的确切替代方案,但我认为这是一种有效的解决方法 . 希望它能帮到你 .

  • 0

    有不同的策略,如何使用mongo创建自动增量序列 . 您可以在官方文档中找到一般信息http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

    这是对sails.js水线的第一种方法的改编,它使用了计数器集合 . 首先,您必须创建序列模型并实现get next方法 .

    module.exports = {
        attributes : {
            num : {
                type : "integer"
            },
        },
    
        next : function (id, cb) {
    
            Sequence.native(function (err, col) {
    
                col.findAndModify(
                    { _id: id },
                    [['_id', 'asc']],
                    {$inc: { num : 1 }},
                    { new: true, upsert : true}
                    , function(err, data) {
    
                        cb(err, data.value.num);
                    });
    
            });
    
        },
    };
    

    由于水线不支持mongodb的findAndModify功能,因此必须使用本机驱动程序方法 . 它看起来有点奇怪,但它确实有效 . 更多关于它here .

    然后,您可以在模型的beforeCreate生命周期回调方法中调用Sequence.next(),以获取新集合文档的下一个自动增量值 .

    // Model Order, for Example.
    module.exports = {
    
        attributes: {
    
            number: {
                type: "integer"
            },
            // other attributes
        },
    
        // add auto increment value for "number" before create a document
        beforeCreate : function (values, cb) {
    
            // add seq number, use
            Sequence.next("order", function(err, num) {
    
                if (err) return cb(err);
    
                values.number = num;
    
                cb();
            });
        }
    
        // your other methods ...
    };
    

    我知道这有点晚了,但我刚刚为我解决了这个问题并希望分享 .

  • 7

    对于那些对使用Native in Sails实现感兴趣的人来说,这是一个有效的示例 . 该示例假定您有一个Counters集合:

    Counters.native(function(err, collection) {
    
            collection.findAndModify(
              {"_id": "order_id"}, // query
              [['_id','asc']],  // sort order
              { "$inc": { "seq": 1 }}, // increments the seq
              {"new": true}, // options
              function(err, object) {
                  if (err){
                      sails.log.debug("ERROR",err.message);  // returns error if no matching object found
                  }else{
                      sails.log.debug(object);
                  }
              });
            });
    
  • 1

    这是一个适用于Sails v1的解决方案

    // api/models/Sequence.js
    
    module.exports = {
      attributes: {
        num: { type: 'number' }
      },
      next (id, cb) {
        var db = Sequence.getDatastore().manager
        var collection = db.collection('num')
    
        collection.findAndModify(
          { _id: id },
          [[ '_id', 'asc' ]],
          { $inc: { num : 1 }},
          { new: true, upsert : true },
          (err, data) => cb(err, data.value.num)
        )
      }
    }
    
    // api/models/Order.js
    
    module.exports = {
      attributes: {
        number: { type: 'number' },
      },
      beforeCreate (obj, cb) {
        Sequence.next('order', (err, num) => {
          if (err) return cb(err)
          obj.number = num
          cb()
        })
      }
    }
    

相关问题