在 mongo 中的日期之间的数组中查找 [英] find in array between dates in mongo
本文介绍了在 mongo 中的日期之间的数组中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 mongo 文档:
I have mongo documents:
{
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}}] )
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. 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屋!
查看全文