数组中的芒果搜索 [英] Mango search in Arrays
问题描述
我的文档结构如下:
{
"Calibration": {
"Presettings": {
"Date": [
{
"Value": "2016-09-02 10:11",
"Type": "generated"
},
{
"Value": "2016-09-05",
"Type": "schedule",
"Duration": "5"
}
]
}
}
}
我必须如何定义查询对象的选择器部分才能获取日期(值)小于或等于给定日期且具有 Type=='generated'
的所有文档?
How must I define the selector part of a query object to get all documents with dates (Value) less or equal to a given date and with Type=='generated'
?
推荐答案
首先,您需要创建索引.我建议您在 Calibration.Presettings.Date 字段
上创建一个索引.
First, you need to create your index. I suggest that you create an index on the Calibration.Presettings.Date field
.
您可以使用以下 JSON 对象来创建它:
You can use the following JSON object to create it:
{
"index": {
"fields": [
"_id",
"Calibration.Presettings.Date.[].Type"
]
},
"type": "json"
}
所以选择器应该是这样的:
So the selector would be like this :
{
"selector": {
"Calibration.Presettings.Date": {
"$elemMatch": {
"$and": [
{
"Type": "generated"
},
{
"Value": {
"$gte": "2016-09-01"
}
}
]
}
}
}
}
我们在字段 Calibration.Pressettings.Date 上执行查询,这是一个 数组.由于它是一个数组,我们必须使用 $elemMatch 运算符.
We execute the query on the field Calibration.Pressettings.Date which is an Array. Since it's an array, we have to use the $elemMatch operator.
然后,我们有一个用于值和类型的$and条件.
Then, we have a $and condition for the Value and the Type.
日期的类型必须生成.With 既可以使用 $eq 运算符,也可以简单地使用这个简单的语法:{"field":"value"}
.
The Type of the Date has to be generated. With can either use the $eq operator or simply use this simple syntax: {"field":"value"}
.
最后,Date`s Value 必须大于或等于 X 日期.我们可以使用 $gte 运算符.
Finally, the Date`s Value must be greater or equal to X date. We can use the $gte operator.
这篇关于数组中的芒果搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!