Mongodb子文档日期范围返回错误的结果 [英] Mongodb Subdocument Date Range Returns Wrong Results

查看:125
本文介绍了Mongodb子文档日期范围返回错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Stackoverflow. 我正在尝试在数组集合上运行日期范围查询,但是Mongo Shell返回的无关文档不符合我的条件.没关系,我正在通过PHP驱动程序,Doctrine Mongodb Query-builder或Mongo Shell进行查询.

first time in Stackoverflow. I'm trying to run a date range query on an array collection but Mongo Shell returning irrelevant documents witch doesn't match my criteria. It doesn't matter i'm doing the query trough PHP drivers, Doctrine Mongodb Query-builder or 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"符号等,但没有结果.

I'm trying to query documents with "total_sold.created_at" fields are set to "2014-03-05" but the returning result doesn't include any sub-collections created at "2014-03-05", what's the point am I missing? I've tried $and operator, "$total_sold.created_at" notation etc. but no results.

Ps:total_sold.created_at字段已编制索引.

Ps: total_sold.created_at field is indexed.

我已经通过Doctrine-Mongodb创建并保存了我的文档.这是我的Doctrine-Mongodb映射.对于主文档和total_sold子文档.

I've created and persisted my documents via Doctrine-Mongodb. Here are my Doctrine-Mongodb mappings. For the master document and total_sold sub-document.

<?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;
    }


}

推荐答案

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

You have to use $elemMatch operator for matching more than one component within an array element with $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 Doc: http://docs.mongodb.org/manual /reference/operator/query/elemMatch/

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

这篇关于Mongodb子文档日期范围返回错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆