【问题标题】:Auto-increment in MongoDB with CodeIgniter Alex Bilbie library使用 CodeIgniter Alex Bilbie 库在 MongoDB 中自动递增
【发布时间】:2013-07-21 12:02:06
【问题描述】:

首先,对不起我的英语,我开始学习 MongoDB。 :)

我正在尝试使用 CI 库将记录插入 MongoDB (https://github.com/alexbilbie/codeigniter-mongodb-library/tree/v2)。

插入工作完美,但我无法使用推荐的函数插入以自动递增 getNextSequence (http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/)。

我尝试了以下方法都没有成功。

控制器:

$data = array('_id' => 'getNextSequence("myid")', 
              'name' => 'Test '.time(),
              'email' => 'test@test.com');
$this->default_model->add($this->collection, $data);

型号:

function add($collection, $data){
    return $this->mongo_db->insert($collection, $data);
}

这会返回字符串 'getNextSequence("relatoriosid")' 作为“_id”。

我也试过用command功能,像这样:

控制器:

$query = 'db.collection.insert({_id: getNextSequence("myid"), name: "Test '.time().'"});';
$ret = $this->default_model->execute($query);
var_dump($ret);

型号:

function execute($query){
    return $this->mongo_db->command($query);
}

这样,返回错误:

["errmsg"]=> string(136) "exception: JavaScript execution failed: ReferenceError: getNextSequence is not defined near 'quence("myid"), name: "Teste 1374' "
["code"]=> int(16722)
["ok"]=> float(0)

有没有人实现过类似的东西? 提前致谢!

【问题讨论】:

标签: php codeigniter mongodb auto-increment


【解决方案1】:

我在我的一个项目中实施了一次 mongoDB。 我遇到过类似的问题。但我记得一件重要的事情是我们不能修改_id。它的MongoDB 正确内置。在MongoDB 中拥有auto-increament 也不好,因为您需要手动实现它。

我会建议在结构中使用另一列,您可以将其命名为任何名称,这将是您的虚拟唯一/主键,假设列名称是 data_id

$data = array('data_id' => md5(timestamp() . "<your string>"), 
              'name' => 'Test '.time(),
              'email' => 'test@test.com');

如果我们检查文档,指定Generally in MongoDB, you would not use an auto-increment pattern for the _id field, or any field, because it does not scale for databases with large numbers of documents. Typically the default value ObjectId is more ideal for the _id. 这里我指的是this link

即使在同一个链接上,它也指定getNextSequence() 我们需要编写它。它不是内置函数。

希望对你有所帮助。

【讨论】:

  • 修改_id是对的,但是如果我想在运行时的插入中调用某个函数并自动递增,是不可能的吗?
  • 好的.. docs.mongodb.org/manual/reference/method/cursor.max 请检查此链接。正如你所说,我们可以修改_id,开始将整数值添加到_id。每次插入前,选择max(_id)记录,递增,插入数据库。
【解决方案2】:

我也遇到了这个问题,这是我的解决方案。

将此函数添加到 appflow\application\libraries

    public function getNextSequence($name = ""){
    if (empty($name))
    {
        show_error("In order to retrieve documents from MongoDB, a collection name must be passed", 500);
    }
    try{
        $collection_1 = 'counters';
        $collection = $this->db->{$collection_1};
    $result =  $collection->findAndModify(
        ['_id' => $name],
        ['$inc' => ['seq' => 1]],
        ['seq' => true],
        ['new' => true, 'upsert' => true]
    );
    if (isset($result['seq']))
    {
        return $result['seq'];
    }
    else
    {
        return false;
    }

    }       
    catch (MongoCursorException $e)
    {
        if(isset($this->debug) == TRUE && $this->debug == TRUE)
        {
            show_error("MongoDB query failed: {$e->getMessage()}", 500);
        }
        else
        {
            show_error("MongoDB query failed.", 500);
        }
    }
}

这是使用方法。

$nextID = $this->mongo_db->getNextSequence("maindata_child");

$this->mongo_db->where(array('_id'=>$mongo_id))->push('maindata_child', array('id'=>$nextID,'data'=>$maindata_child))->update('main_data');

别忘了添加集合“计数器”。

    db.counters.insert(
   {
      _id: "userid",
      seq: 0
   }
)

附言。我正在使用这个库https://github.com/bcit-ci/CodeIgniter/wiki/Using-MongoDB-in-Codeigniter 解决方案参考:http://tbsmc.com/a/mwbwswmm-mongodb-how-to-insert-record-using-autoincrement-functionality.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2016-01-29
    • 2016-09-06
    • 2017-01-11
    • 2013-08-24
    • 2015-02-18
    • 2018-01-17
    • 2016-03-07
    相关资源
    最近更新 更多