数组中的芒果搜索 [英] Mango search in Arrays
问题描述
我的文档具有这样的结构:
My document has a structure like this:
{
"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 field
上创建一个索引.
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"
}
所以选择器将像这样:
{
"selector": {
"Calibration.Presettings.Date": {
"$elemMatch": {
"$and": [
{
"Type": "generated"
},
{
"Value": {
"$gte": "2016-09-01"
}
}
]
}
}
}
}
我们在 Calibration.Pressettings.Date 字段上执行查询,该字段是 Array .由于它是一个数组,因此我们必须使用 $ 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.
然后,对于值和类型,我们有一个 $和条件.
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"}
.
最后,日期"的值必须大于或等于X日期.我们可以使用 $ gte 运算符.
Finally, the Date`s Value must be greater or equal to X date. We can use the $gte operator.
这篇关于数组中的芒果搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!