【问题标题】:MongoDB not insert in order with PHP DriverMongoDB 未使用 PHP 驱动程序按顺序插入
【发布时间】:2015-11-30 08:44:56
【问题描述】:

插入 MongoDB 数据库时出现问题。它没有按正确的顺序插入数据库。

我们阅读了 MongoDB 中的写作问题: http://www.php.net/manual/en/mongo.writeconcerns.php

我们使用 mongoDB 2.6 和 PHP 驱动程序 1.6 以及以下示例代码:

set_message_sample('1');
set_message_sample('2');

set_message_sample($id) {
$connecting_string = sprintf('mongodb://%s:%d/%s', $hosts, $port,$database), // fill with right connection setting
$connection=  new Mongo($connecting_string,array('username'=>$username,'password'=>$password)); // fill with proper authentication setting

$dbname = $connection->selectDB('dbname');

$collection = $dbname->selectCollection('collection');
$post = array(
    'title' => $id,
    'content' => 'test ' . $id,
);
$posts->insert($insert,array("w" => "1"));

有时结果是在“1”之前插入“2”到数据库。我们希望始终以正确的顺序插入(第一个“1”和下一个“2”)。我还注意到我们使用由 mongoDB 自动设置的 mongoID 订购集合。

我们检查了许多选项,但问题仍未解决。我们怎么能解决它? (我们如何禁用缓存之类的东西或将插入队列隔离到 MongoDB。)

【问题讨论】:

  • 发布您的代码和错误。
  • 集合中的项目没有固有的“顺序”。您可以在查询时订购商品。您为什么/如何认为您的物品“顺序错误”?!
  • 我们使用 MongoID 订购集合

标签: php mongodb


【解决方案1】:

所以,我认为您只能在第一个确认后插入第二个。由于插入是异步的,因此您无法确定谁先进行。所以必须在第一个确认后才可以插入。

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2016-03-13
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多