使用MongoDB进行审批/挂起


Using MongoDB for approved / pending

我有一个客户希望获得合作伙伴的报价集合,但他们需要批准更改。什么是向他们的伴侣隐瞒变化历史的最佳方式?

以下是的工作原理

{
partner_id: {here}
offer1:
offer2:
offer3:
date:
status:
}

我需要一种方法来检查是否有任何报价发生了变化(但只有1个回复),如果发生了变化,插入并将过去的一个作为状态:历史

应该只有以下状态:

历史记录
挂起
批准的

但是,如果他们正在插入一个新的报价,但有一个已批准的报价,那么在我们批准之前,它需要保持批准状态。

我该怎么做?我是这样想的:

    <?php
    function checkoffers($data)
    {
    $this->data = $data;
    $collection = $this->db->partner_offers;
    if($this->data["_id"] != null)
    {
      $cursor = $collection->insert(RUN INSERT ARRAY)
    }
    else
    {
      $cursor = $collection->find(array("_id"=>new MongoId($this->data["_id"])));
      if ($cursor->count() > 0)
        {
            $test = array();
            while( $cursor->hasNext() ) {   
                $test[] = ($cursor->getNext());
            }

                     foreach($test as $offers)
                     {
                      check to see if offer matches the past offer. if not then run new function and insert.
}

                 }

    }
?>

一种方法是用每个报价的文档来组织您的模式:

{
    partner_id: 123,
    offer: 'Even better than the last one',
    date: ISODate("2012-08-28T10:00"),
    status: 'pending'
}
{
    partner_id: 123,
    offer: 'The best of the best',
    date: ISODate("2012-08-28T09:00"),
    status: 'approved'
}
{
    partner_id: 123,
    offer: 'An offer you cannot refuse',
    date: ISODate("2012-08-28T08:00"),
    status: 'approved'
}

然后,您可以很容易地找到按状态和日期排序的最新批准或待定报价:

> db.partner.find({partner_id:123, status:'approved'}).sort({date:-1}).limit(1)
{
    "_id" : ObjectId("503c6c8be16d4a06b6f0da17"),
    "partner_id" : 123,
    "offer" : "The best of the best",
    "date" : ISODate("2012-08-28T09:00:00Z"),
    "status" : "approved"
}