我有一个带有嵌入式子命令的订单。我正在请求只找到今天发送的子订单。当我使用带有两个子句的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})
答案 0 :(得分:0)
我最终使用了聚合方法。无论出于何种原因,这允许我在elem_match没有时查询gte和lte。
orders = Order.collection.aggregate([
{"$unwind" => "$sub_orders"},
{"$match" => {
"sub_orders.sent_at" => {"$gte" => day_ago, "$lte" => now}
}}
])