【问题标题】:How to create auto increment field如何创建自增字段
【发布时间】:2012-04-12 21:28:38
【问题描述】:

我想使用 php 在 mongodb 的字段中添加自动增量值,而不使用计数记录,下一个数字(计数 + 1)将是我的增量值... 或者有什么方法可以在mongodb,php 中创建自动增量值数组,例如一个客户有多个电子邮件和地址,然后我们可以像email[{1:'a@x.com',2:'demo@example.com'},...] 一样存储它,对于address[{1:'Indore',2:'Dewas'},...] 也是一样的所以为了唯一性我想从每个电子邮件和地址设置自动增量值..

【问题讨论】:

    标签: php laravel mongodb


    【解决方案1】:

    如果你创建两个collections是可能的

    首先存储您要为其创建自增数之类的字段

    // Inserting 2 records for which we want to create auto-increment numbers
    db.autoincrementcollection.insert({
       field_id: "register_id",
       nextId: 0
    },
    {
       field_id: "user_id",
       nextId: 0
    });
    
    // Function is used to get the next autoincremented value
    function getNextId(field)
    {
       var ret = db.autoincrementcollection.findAndModify({
            query: { field_id: field },
            update: { $inc: { nextId: 1 } },
            new: true
       });    
       return ret.nextId;
    }
    

    Second你想在其中使用递增的数字

    // inserting data in registration collection with register_id auto-increment number
    db.registration.insert({
         _id: getNextId("register_id"),
         fname: "Rohan",
         lname:"Kumar"
    });
    // inserting data in user collection with user_id auto-increment number
    db.users.insert({
         _id: getNextId("user_id"),
         name: "Rohan Kumar"
    });
    

    findAndModify() 可能在使用 PHP 的代码时不起作用,那么您可以使用 find() 和/或 update()

    请参阅文档了解create-an-auto-incrementing-field

    【讨论】:

    • 如何使用 PHP 实现上述功能?
    【解决方案2】:

    有可能我们必须获取最大 id 表单集合

    db -> collection -> find() -> sort(array('id' => -1)) -> limit(1) 
    

    它返回最大 id 数组。添加 db['id'] + 1;

    每次都不需要在mongo中存储自增id

    【讨论】:

      【解决方案3】:

      理想情况下,您不应该在 MongoDb 中使用自动增量作为标识符。 如果您愿意,请参考此处给出的 2 个好方法:

      http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field

      【讨论】:

        【解决方案4】:

        这是我在 MongoDB PHP 库中创建自动增量 id 的方法:

        private function get_last_id()
            {
                // put your connection string blow
                $client = new MongoDB\Client('');
                $counterCollection = $client->collection->counter;
                $counter["id"]=1;
                if ($counterCollection->count()<=0) {
                    $insertOneResult = $counterCollection->insertOne($counter);
                } else {
                    $filter  = [];
                    $options = ['sort' => ['_id' => -1], 'limit' => 1];
                    $doc = $counterCollection->findOne($filter, $options);
                    $id=$doc->id+1;
                    $updateResult = $counterCollection->updateOne(
                    [ '_id' => $doc->_id ],
                    [ '$set' => [ 'id' =>$id ]]
                );
                    return $id;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-30
          • 2017-06-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-03
          • 2015-03-06
          • 1970-01-01
          相关资源
          最近更新 更多