【发布时间】:2014-08-03 14:47:55
【问题描述】:
我有如下格式的数据结构:
{
topAttribute: {
subAttribute:
[
{ eventType: "SPECIAL", date: "20121231" }
{ eventType: "NOTSPECIAL", date: "20131012" }
{ eventType: "NOTSPECIAL", date: "20131122" }
...
]
}
}
我正在尝试识别 eventType 为“SPECIAL”且日期为“20121231”的记录。我正在尝试使用以下多键样式查询来完成此操作:
Mongoid:
Item.where('topAttribute.subAttribute.eventType' => 'SPECIAL').and('topAttribute.subAttribute.date' => '20121231').all
MongoDB:
db.items.find( { 'topAttribute.subAttribute.eventType': 'SPECIAL', 'topAttribute.subAttribute.date': '20121231'} )
但是,当我运行此查询时,它会发现 either 记录的 eventType 为“SPECIAL”OR 记录的日期为“20121231”。这不是我想要的结果。
我如何重组这个查询——或者使用其他搜索/聚合策略——只显示事件类型为“SPECIAL”的记录AND日期为“20121231”的记录,并排除所有其他的?
【问题讨论】:
标签: mongodb mongoid mongodb-query