Elem_match没有在mongoid请求中链接“和”

时间:2016-04-27 10:13:55

标签: mongoid mongodb-query

我有一个带有嵌入式子命令的订单。我正在请求只找到今天发送的子订单。当我使用带有两个子句的elem_match时,它似乎是包含在内的。这对我来说似乎很奇怪,因为在where子句中排列是独占的。有没有办法让elem_match条件排除?

first_minute = Time.mktime(Time.now.year, Time.now.month, Time.now.day,  0, 0)
last_minute  = Time.mktime(Time.now.year, Time.now.month, Time.now.day,  0, 59)

# Fails sent_at.gte but returns the order anyways
orders = Order.where(:sub_orders.elem_match => {:sent_at.gte => first_minute, :sent_at.lte => last_minute })

# Fails sent_at.gte and returns what I would expect
orders = Order.where(:sub_orders.elem_match => {:sent_at.gte => first_minute})

1 个答案:

答案 0 :(得分:0)

我最终使用了聚合方法。无论出于何种原因,这允许我在elem_match没有时查询gte和lte。

orders = Order.collection.aggregate([
  {"$unwind" => "$sub_orders"},
  {"$match" => {
    "sub_orders.sent_at" => {"$gte" => day_ago, "$lte" => now}
  }}
])