在mongo中的日期之间找到数组 [英] find in array between dates in mongo
本文介绍了在mongo中的日期之间找到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有mongo文件:
{
id:1
time:[
1,
2,
10
]
}
{
id:2
time:[
1,
4,
8,
10
]
}
我想查找所有时间在3到5或7到9之间的文档(ID为2的文档).我不知道该怎么办.
I want find all documents, which have time between 3 and 5 or between 7 or 9 (document with id 2). I dont know how do this.
我尝试:
mongo.find( $or : [{time:{$gte:3, $lte:5}}, {time:{$gte:7, $lte:9}}] )
and mongo返回id为1和2的文档,但是我需要id2为time [4,8].
and mongo returns documents with id 1 and 2, but i need id2 with time[4, 8].
如何将条件应用于数组的每个元素,而不应用于整个数组?
How to apply a condition to each element of the array, but NOT to entire array?
p.s.大小数组时间"可能有所不同
p.s. Size array "time" maybe different
推荐答案
尝试使用 $ elemMatch 运算符:
mongo.find({time:{$elemMatch: {$gte:3,$lte:5}}})
喜欢:
mongo.find(
{
$or: [
{time:{$elemMatch: {$gte:3,$lte:5}}},
{time:{$elemMatch: {$gte:7,$lte:9}}}
]
}
)
这篇关于在mongo中的日期之间找到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文