【问题标题】:MongoDB auto-increment counter document with Doctrine ODMMongoDB自增计数器文档与Doctrine ODM
【发布时间】:2013-12-23 16:40:23
【问题描述】:

我需要 MongoDB 中的自动递增字段。 我找到了documentation on how to use a counter-collection in MongoDB

但是,我的值 id 不是 _id,它不一定是设置的,我需要通过 Doctrine ODM 使用它(我不太熟悉)。我如何通过 Doctrine ODM 使用反集合?

【问题讨论】:

    标签: doctrine-odm


    【解决方案1】:

    不是我所希望的,但分两步完成是我找到的唯一解决方案:

    $counter = $dm->createQueryBuilder('Documents\Counter')
    ->findAndUpdate()
    ->returnNew()
    ->field('name')->equals('order_number')
    ->field('count')->inc(1)
    ->getQuery()
    ->execute();
    

    然后在另一个文档中使用返回值:

    $order = $dm->getRepository('Documents\Order')->findOneBy(array('id' => <some_id>));
    $order->setOrderNumber($counter->getCount());
    ...
    

    【讨论】:

      【解决方案2】:

      抱歉,我没有足够的评论点。您可以在不使用计数器集合的情况下拥有一个自动增量字段。您可以使用 INCREMENT Doctrine Annotation marker 作为 Doctrine mongoDB 文档类中的 feild 变量。 看这里:http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html#identifiers

      【讨论】:

      • 在 cakePhp 中我也很难生成自动增量,但后来我知道对于具有大量文档的可扩展数据库,这是不可取的。 “警告通常在 MongoDB 中,您不会对 _id 字段或任何字段使用自动递增模式,因为它不适用于具有大量文档的数据库。通常默认值 ObjectId 更适合_id. docs.mongodb.org/manual/tutorial/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 2012-12-20
      • 2014-03-20
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多