Mongodb子文档日期范围返回错误的结果

时间:2014-03-09 19:12:42

标签: mongodb date-range doctrine-mongodb mongo-collection subdocument

Stackoverflow中的第一次。 我正在尝试对数组集合运行日期范围查询,但Mongo Shell返回的无关文档与我的标准不符。我通过PHP驱动程序,Doctrine Mongodb Query-builder或Mongo Shell进行查询并不重要。

这是我的问题:

db.deals.find( { "total_sold.created_at": 
                    { $gt: new ISODate("2014-03-05T00:00:00Z"),
                      $lt: new ISODate("2014-03-05T23:59:00Z") 
                    }
               }).limit(1).pretty()

结果如下:

    {
    "_id" : "1241412fb99a11a0bc70032a2cb6059b",

    "total_sold" : [
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-13T15:48:35Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-14T10:26:19Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-15T11:36:50Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-17T09:35:19Z"), 
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-19T16:34:52Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-21T12:06:10Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-02-24T09:52:23Z"),
        },
        {
            "some_field": "value",
            "created_at" : ISODate("2014-03-07T22:40:37Z"),
        }
    ],
    "updated_at" : ISODate("2014-03-07T22:40:40Z")
}    

我正在尝试查询“total_sold.created_at”字段设置为“2014-03-05”的文档,但返回的结果不包含在“2014-03-05”创建的任何子集合,这是什么我错过了什么?我已经尝试了$和运算符,“$ total_sold.created_at”符号等,但没有结果。

Ps:total_sold.created_at字段已编入索引。

编辑: 我通过Doctrine-Mongodb创建并保存了我的文档。这是我的Doctrine-Mongodb映射。对于主文档和total_sold子文档。

<?php

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;


/**
 * @ODM\Document(
 *   collection="deals",
 * )
 * @ODM\Index(keys={"total_sold"="desc"}),
 */
Class Deal {

    /**
     * @ODM\Id(strategy="NONE")
     */
    protected $id;

    /**
    *Some other fields
    **/


    /**
     * @ODM\EmbedMany(targetDocument="DealTotalSold")
     */
    protected $total_sold;


    /**
     * Constructor
     */
    public function __construct() {
        $this->total_sold = new \Doctrine\Common\Collections\ArrayCollection();
    }



    /**
     * Add totalSold
     *
     */
    public function addTotalSold($totalSold) {
        $this->total_sold[] = $totalSold;
    }

    /**
     * Remove totalSold
     *
     */
    public function removeTotalSold($totalSold) {
        $this->total_sold->removeElement($totalSold);
    }

    /**
     * Get totalSold
     *
     * @return Doctrine\Common\Collections\Collection $totalSold
     */
    public function getTotalSold() {
        return $this->total_sold;
    }

}

这是我的子文件。

<?php

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ODM\EmbeddedDocument
 */
Class DealTotalSold {

    /**
     * @ODM\Id(strategy="NONE")
     */
    protected $id;


    /**
     * @ODM\Date
     * @Gedmo\Timestampable(on="create")
     */
    protected $created_at;

    /**
     * @ODM\Int
     */
    protected $delta_totalsold;



    public function __construct()
    {

        }

    /**
     * Set createdAt
     *
     * @param date $createdAt
     * @return self
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;
        return $this;
    }

    /**
     * Get createdAt
     *
     * @return date $createdAt
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set dealtaTotalsold
     *
     * @param Int $dealtaTotalsold
     * @return self
     */
    public function setDeltaTotalsold($dealtaTotalsold)
    {
        $this->delta_totalsold = $dealtaTotalsold;
        return $this;
    }



    /**
     * Set id
     *
     * @param custom_id $id
     * @return self
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * Get id
     *
     * @return custom_id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Get deltaTotalsold
     *
     * @return float $deltaTotalsold
     */
    public function getDeltaTotalsold()
    {
        return $this->delta_totalsold;
    }


}

1 个答案:

答案 0 :(得分:1)

您必须使用$ elemMatch运算符来匹配数组元素中的多个组件和$和

db.deals.find({ total_sold: { $elemMatch : {
  $and:[ {created_at: {$gt: new ISODate("2014-03-05T00:00:00Z") }},
         { created_at: {$lt: new ISODate("2014-03-05T23:00:00Z") }}
        ]
  }}
});

$ elemMatch Doc:http://docs.mongodb.org/manual/reference/operator/query/elemMatch/