来自shell的mongo查询

时间:2017-03-11 06:12:48

标签: mongodb shell

我正在直接从shell执行此查询。

namespace Domain\Ordering;

class Cart
{
    /** @var CartItem[] */
    private $items = [];

    const MAXIMUM_CART_VALUE = 10000;

    public function handleAddItemToCart(AddItemToCart $command)
    {
        if ($this->getTotalCartValue() + $command->getItem()->getTotalItemPrice() > self::MAXIMUM_CART_VALUE) {
            throw new \Exception(sprintf("A cart value cannot be more than %d USD", self::MAXIMUM_CART_VALUE));
        }

        yield new AnItemWasAddedToCart($command->getCartId(), $command->getItem());
    }

    public function applyAnItemWasAddedToCart(AnItemWasAddedToCart $event)
    {
        $this->items[] = $event->getItem();
    }

    private function getTotalCartValue()
    {
        return array_reduce($this->items, function (float $acc, CartItem $item) {
            return $acc + $item->getTotalItemPrice();
        }, 0.0);
    }
}

class AddItemToCart implements Command
{

    /**
     * @var CartId
     */
    private $cartId;
    /**
     * @var CartItem
     */
    private $item;

    public function __construct(
        CartId $cartId,
        CartItem $item
    )
    {
        $this->cartId = $cartId;
        $this->item = $item;
    }

    public function getCartId(): CartId
    {
        return $this->cartId;
    }

    public function getItem(): CartItem
    {
        return $this->item;
    }
}

class AnItemWasAddedToCart implements Event
{
    /**
     * @var CartId
     */
    private $cartId;
    /**
     * @var CartItem
     */
    private $item;

    public function __construct(
        CartId $cartId,
        CartItem $item
    )
    {
        $this->cartId = $cartId;
        $this->item = $item;
    }

    public function getCartId(): CartId
    {
        return $this->cartId;
    }

    public function getItem(): CartItem
    {
        return $this->item;
    }
}

class CartId
{
    //...
}

class ProductId
{
    //...
}

class CartItem
{
    /**
     * @var ProductId
     */
    private $productId;
    /**
     * @var float
     */
    private $pricePerUnit;
    /**
     * @var int
     */
    private $quantity;

    public function __construct(
        ProductId $productId,
        float $pricePerUnit,
        int $quantity
    )
    {
        $this->productId = $productId;
        $this->pricePerUnit = $pricePerUnit;
        $this->quantity = $quantity;
    }

    public function getProductId(): ProductId
    {
        return $this->productId;
    }

    public function getTotalItemPrice()
    {
        return $this->pricePerUnit * $this->quantity;
    }
}

它们不是来自此查询的输出。

查询在连接时卡住了

1 个答案:

答案 0 :(得分:0)

确保您的“date_time”属性是日期类型。 还要在字段上添加适当的索引。

并且在使用日期进行范围查询时更好地使用ISODate而不是字符串。

对于Ex:

items.find({
    created_at: {
        $gte: ISODate("2010-04-29T00:00:00.000Z"),
        $lt: ISODate("2010-05-01T00:00:00.000Z")
    }
})