1

第一次在 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”创建的任何子集合,这是什么意思?我失踪了?我尝试过 $and 运算符、“$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;
    }


}
4

1 回答 1

1

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

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 文档:http ://docs.mongodb.org/manual/reference/operator/query/elemMatch/

于 2014-03-09T20:30:34.300 回答